Example usage for org.apache.poi.xssf.usermodel XSSFRichTextString getLengthOfFormattingRun

List of usage examples for org.apache.poi.xssf.usermodel XSSFRichTextString getLengthOfFormattingRun

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRichTextString getLengthOfFormattingRun.

Prototype

public int getLengthOfFormattingRun(int index) 

Source Link

Document

Returns the number of characters this format run covers.

Usage

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private boolean hasSuperscriptText(Cell excelCell) {
    if (excelCell.getCellType() != Cell.CELL_TYPE_STRING)
        return false;

    RichTextString richTextString = excelCell.getRichStringCellValue();
    if (null == richTextString)
        return false;

    int index = 0;
    int length = 0;
    XSSFFont font = null;// w  ww  .  j  a va2s  . c o  m

    XSSFRichTextString rts = (XSSFRichTextString) richTextString;
    XSSFWorkbook wb = (XSSFWorkbook) workbook;

    XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle();
    font = style.getFont();

    String richText = rts.getString();
    if (rts.numFormattingRuns() > 1) {
        for (int i = 0; i < rts.numFormattingRuns(); i++) {
            index = rts.getIndexOfFormattingRun(i);
            length = rts.getLengthOfFormattingRun(i);

            try {
                font = rts.getFontOfFormattingRun(i);
            } catch (NullPointerException e) {
                font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET);
                font.setTypeOffset(XSSFFont.SS_NONE);
            }

            String s = richText.substring(index, index + length);

            if (font.getTypeOffset() == XSSFFont.SS_SUPER)
                return true;
        }
    } else {
        if (font.getTypeOffset() == XSSFFont.SS_SUPER)
            return true;
    }
    return false;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private String getNotSuperscriptText(Cell excelCell) {
    if (excelCell.getCellType() != Cell.CELL_TYPE_STRING)
        return null;
    RichTextString richTextString = excelCell.getRichStringCellValue();
    if (null == richTextString)
        return null;

    int index;/*from ww  w.j a v  a 2 s.  co m*/
    int length;
    String text;

    XSSFRichTextString rts = (XSSFRichTextString) richTextString;
    XSSFWorkbook wb = (XSSFWorkbook) workbook;

    XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle();
    XSSFFont font = style.getFont();

    String richText = rts.getString();
    StringBuilder notSuperscriptRuns = new StringBuilder();
    if (rts.numFormattingRuns() > 1) {
        boolean wasNotSuperscriptRun = false;
        for (int i = 0; i < rts.numFormattingRuns(); i++) {
            index = rts.getIndexOfFormattingRun(i);
            length = rts.getLengthOfFormattingRun(i);

            try {
                font = rts.getFontOfFormattingRun(i);
            } catch (NullPointerException e) {
                font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET);
                font.setTypeOffset(XSSFFont.SS_NONE);
            }

            String s = richText.substring(index, index + length);

            if (font.getTypeOffset() == XSSFFont.SS_SUPER) {
                if (wasNotSuperscriptRun)
                    notSuperscriptRuns.append(" ");
                wasNotSuperscriptRun = false;
            } else {
                notSuperscriptRuns.append(s);
                wasNotSuperscriptRun = true;
            }
        }
        text = notSuperscriptRuns.toString();
    } else {
        if (font.getTypeOffset() == XSSFFont.SS_SUPER)
            text = null;
        else
            text = richText;
    }
    return text;
}