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

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

Introduction

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

Prototype

public int getIndexOfFormattingRun(int index) 

Source Link

Document

The index within the string to which the specified formatting run applies.

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;/*from  ww w  .j a v  a 2 s  . 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  www. j a  va 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;
}