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

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

Introduction

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

Prototype

public XSSFFont getFontOfFormattingRun(int index) 

Source Link

Document

Gets a copy of the font used in a particular formatting run.

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;/*  ww  w  . j av 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;/*w  w w.  j a  v a  2  s.c o  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;
}