Search in sources :

Example 11 with RichTextString

use of org.apache.poi.ss.usermodel.RichTextString in project poi by apache.

the class SheetUtil method getCellWidth.

/**
     * Compute width of a single cell
     *
     * @param cell the cell whose width is to be calculated
     * @param defaultCharWidth the width of a single character
     * @param formatter formatter used to prepare the text to be measured
     * @param useMergedCells    whether to use merged cells
     * @return  the width in pixels or -1 if cell is empty
     */
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
    Sheet sheet = cell.getSheet();
    Workbook wb = sheet.getWorkbook();
    Row row = cell.getRow();
    int column = cell.getColumnIndex();
    // FIXME: this looks very similar to getCellWithMerges below. Consider consolidating.
    // We should only be checking merged regions if useMergedCells is true. Why are we doing this for-loop?
    int colspan = 1;
    for (CellRangeAddress region : sheet.getMergedRegions()) {
        if (region.isInRange(row.getRowNum(), column)) {
            if (!useMergedCells) {
                // If we're not using merged cells, skip this one and move on to the next.
                return -1;
            }
            cell = row.getCell(region.getFirstColumn());
            colspan = 1 + region.getLastColumn() - region.getFirstColumn();
        }
    }
    CellStyle style = cell.getCellStyle();
    CellType cellType = cell.getCellTypeEnum();
    // for formula cells we compute the cell width for the cached formula result
    if (cellType == CellType.FORMULA)
        cellType = cell.getCachedFormulaResultTypeEnum();
    Font font = wb.getFontAt(style.getFontIndex());
    double width = -1;
    if (cellType == CellType.STRING) {
        RichTextString rt = cell.getRichStringCellValue();
        String[] lines = rt.getString().split("\\n");
        for (String line : lines) {
            String txt = line + defaultChar;
            AttributedString str = new AttributedString(txt);
            copyAttributes(font, str, 0, txt.length());
            if (rt.numFormattingRuns() > 0) {
            // TODO: support rich text fragments
            }
            width = getCellWidth(defaultCharWidth, colspan, style, width, str);
        }
    } else {
        String sval = null;
        if (cellType == CellType.NUMERIC) {
            // Try to get it formatted to look the same as excel
            try {
                sval = formatter.formatCellValue(cell, dummyEvaluator);
            } catch (Exception e) {
                sval = String.valueOf(cell.getNumericCellValue());
            }
        } else if (cellType == CellType.BOOLEAN) {
            sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
        }
        if (sval != null) {
            String txt = sval + defaultChar;
            AttributedString str = new AttributedString(txt);
            copyAttributes(font, str, 0, txt.length());
            width = getCellWidth(defaultCharWidth, colspan, style, width, str);
        }
    }
    return width;
}
Also used : RichTextString(org.apache.poi.ss.usermodel.RichTextString) AttributedString(java.text.AttributedString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) AttributedString(java.text.AttributedString) CellType(org.apache.poi.ss.usermodel.CellType) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

RichTextString (org.apache.poi.ss.usermodel.RichTextString)11 Cell (org.apache.poi.ss.usermodel.Cell)7 Sheet (org.apache.poi.ss.usermodel.Sheet)7 Row (org.apache.poi.ss.usermodel.Row)6 Workbook (org.apache.poi.ss.usermodel.Workbook)5 FileOutputStream (java.io.FileOutputStream)4 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)4 Test (org.junit.Test)4 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)3 Comment (org.apache.poi.ss.usermodel.Comment)3 Font (org.apache.poi.ss.usermodel.Font)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 BaseTestCellComment (org.apache.poi.ss.usermodel.BaseTestCellComment)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 CellType (org.apache.poi.ss.usermodel.CellType)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 SpreadsheetUtils.getAdjustedList (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getAdjustedList)1 SpreadsheetUtils.getStringForCell (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell)1