Example usage for org.apache.poi.ss.usermodel CellStyle getIndex

List of usage examples for org.apache.poi.ss.usermodel CellStyle getIndex

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CellStyle getIndex.

Prototype

short getIndex();

Source Link

Document

get the index within the Workbook (sequence within the collection of ExtnededFormat objects)

Usage

From source file:cn.edu.zucc.chenxg.preview.ToHtml.java

License:Apache License

private String styleName(CellStyle style) {
    if (style == null)
        style = wb.getCellStyleAt((short) 0);
    StringBuilder sb = new StringBuilder();
    Formatter fmt = new Formatter(sb);
    try {/*from w  w w  .java 2  s.c  o  m*/
        fmt.format("style_%02x", style.getIndex());
        return fmt.toString();
    } finally {
        fmt.close();
    }
}

From source file:com.common.report.util.html.ToHtml.java

License:Apache License

private String styleName(CellStyle style) {
    if (style == null)
        style = wb.getCellStyleAt((short) 0);
    StringBuilder sb = new StringBuilder();
    Formatter fmt = new Formatter(sb);
    fmt.format("style_%02x", style.getIndex());
    return fmt.toString();
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiStyleHelper.java

License:Open Source License

/**
 * @param cellStyle//from w  w w  .  j av a2  s .  co  m
 *            the internal style
 * @return the style name, ssw<index> if none.
 */
public String getStyleName(final CellStyle cellStyle) {
    final String name = this.cellStyleAccessor.getName(cellStyle);
    if (name == null)
        return "ssw" + cellStyle.getIndex();
    else
        return name;
}

From source file:com.googlecode.testcase.annotation.handle.toexcel.strategy.ToHtmlWithExcel.java

License:Apache License

private String styleName(CellStyle style) {
    if (style == null)
        style = wb.getCellStyleAt((short) 0);
    StringBuilder sb = new StringBuilder();
    @SuppressWarnings("resource")
    Formatter fmt = new Formatter(sb);
    fmt.format("style_%02x", style.getIndex());
    return fmt.toString();
}

From source file:com.hauldata.dbpa.file.book.XlsxTargetSheet.java

License:Apache License

private CellStyle composeCellStyle(Cell cell, Styles styles) {

    CellStyle originalStyle = cell.getCellStyle();

    short formatIndex = originalStyle.getIndex();

    StylesWithFormatting stylesWithFormatting = new StylesWithFormatting(styles, formatIndex);

    return stylesWithFormatting.getCellStyle(getOwner().getBook(), getOwner().stylesUsed, getOwner().fontsUsed,
            getOwner().colorsUsed);/*w w  w.  j ava2  s  .c o  m*/
}

From source file:com.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

protected CellData createCellDataForCell(Cell cell) {
    CellData cellData = new CellData();
    cellData.row = cell.getRowIndex() + 1;
    cellData.col = cell.getColumnIndex() + 1;
    CellStyle cellStyle = cell.getCellStyle();
    cellData.cellStyle = "cs" + cellStyle.getIndex();
    cellData.locked = spreadsheet.isCellLocked(cell);
    try {/*w  w  w  .j a va 2  s.c om*/
        if (!spreadsheet.isCellHidden(cell)) {
            if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                cellData.formulaValue = formulaFormatter.reFormatFormulaValue(cell.getCellFormula(),
                        spreadsheet.getLocale());
                try {
                    String oldValue = getCachedFormulaCellValue(cell);
                    String newValue = formatter.formatCellValue(cell, getFormulaEvaluator());
                    if (!newValue.equals(oldValue)) {
                        changedFormulaCells.add(new CellReference(cell));
                    }
                } catch (RuntimeException rte) {
                    // Apache POI throws RuntimeExceptions for an invalid
                    // formula from POI model
                    String formulaValue = cell.getCellFormula();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue(formulaValue);
                    spreadsheet.markInvalidFormula(cell.getColumnIndex() + 1, cell.getRowIndex() + 1);
                }

            }
        }

        if (cell.getCellStyle().getDataFormatString().contains("%")) {
            cellData.isPercentage = true;
        }

        String formattedCellValue = formatter.formatCellValue(cell, getFormulaEvaluator());

        if (!spreadsheet.isCellHidden(cell)) {
            if (cell.getCellType() == Cell.CELL_TYPE_FORMULA || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                formattedCellValue = formattedCellValue.replaceAll("^-(?=0(.0*)?$)", "");
            }
        }
        if (spreadsheet.isMarkedAsInvalidFormula(cellData.col, cellData.row)) {
            // The prefix '=' or '+' should not be included in formula value
            if (cell.getStringCellValue().charAt(0) == '+' || cell.getStringCellValue().charAt(0) == '=') {
                cellData.formulaValue = cell.getStringCellValue().substring(1);
            }
            formattedCellValue = "#VALUE!";
        }

        if (formattedCellValue != null && !formattedCellValue.isEmpty() || cellStyle.getIndex() != 0) {
            // if the cell is not wrapping text, and is of type numeric or
            // formula (but not date), calculate if formatted cell value
            // fits the column width and possibly use scientific notation.
            cellData.value = formattedCellValue;
            cellData.needsMeasure = false;
            if (!cellStyle.getWrapText()
                    && (!SpreadsheetUtil.cellContainsDate(cell) && cell.getCellType() == Cell.CELL_TYPE_NUMERIC
                            || cell.getCellType() == Cell.CELL_TYPE_STRING
                            || (cell.getCellType() == Cell.CELL_TYPE_FORMULA
                                    && !cell.getCellFormula().startsWith("HYPERLINK")))) {
                if (!doesValueFit(cell, formattedCellValue)) {
                    if (valueContainsOnlyNumbers(formattedCellValue) && isGenerallCell(cell)) {
                        cellData.value = cellValueFormatter.getScientificNotationStringForNumericCell(
                                cell.getNumericCellValue(), formattedCellValue,
                                cellStyleWidthRatioMap.get((int) cell.getCellStyle().getIndex()),
                                spreadsheet.getState(false).colW[cell.getColumnIndex()] - 10);
                    } else if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                        cellData.needsMeasure = true;
                    }
                }
            }

            if (cellStyle.getAlignment() == CellStyle.ALIGN_RIGHT) {
                cellData.cellStyle = cellData.cellStyle + " r";
            } else if (cellStyle.getAlignment() == CellStyle.ALIGN_GENERAL) {
                if (SpreadsheetUtil.cellContainsDate(cell) || cell.getCellType() == Cell.CELL_TYPE_NUMERIC
                        || (cell.getCellType() == Cell.CELL_TYPE_FORMULA
                                && !cell.getCellFormula().startsWith("HYPERLINK")
                                && !(cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING))) {
                    cellData.cellStyle = cellData.cellStyle + " r";
                }
            }

        }

        // conditional formatting might be applied even if there isn't a
        // value (such as borders for the cell to the right)
        Set<Integer> cellFormattingIndexes = spreadsheet.getConditionalFormatter().getCellFormattingIndex(cell);
        if (cellFormattingIndexes != null) {

            for (Integer i : cellFormattingIndexes) {
                cellData.cellStyle = cellData.cellStyle + " cf" + i;
            }

            markedCells.add(SpreadsheetUtil.toKey(cell));
        }

        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)) {
            cellData.originalValue = cellData.value;
        } else {
            cellData.originalValue = getOriginalCellValue(cell);
        }

        handleIsDisplayZeroPreference(cell, cellData);
    } catch (RuntimeException rte) {
        LOGGER.log(Level.FINEST, rte.getMessage(), rte);
        cellData.value = "#VALUE!";
    }

    return cellData;
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

/**
 * Reloads all sheet and cell styles from the current Workbook.
 *//*from  w  ww.ja  v a  2 s . c  o m*/
public void reloadWorkbookStyles() {
    final Workbook workbook = spreadsheet.getWorkbook();
    if (spreadsheet.getState().cellStyleToCSSStyle == null) {
        spreadsheet.getState().cellStyleToCSSStyle = new HashMap<Integer, String>(workbook.getNumCellStyles());
    } else {
        spreadsheet.getState().cellStyleToCSSStyle.clear();
    }
    shiftedBorderLeftStyles.clear();
    shiftedBorderTopStyles.clear();
    mergedCellBorders.clear();

    // get default text alignments
    CellStyle cellStyle = workbook.getCellStyleAt((short) 0);
    defaultTextAlign = cellStyle.getAlignment();
    // defaultVerticalAlign = cellStyle.getVerticalAlignment();

    // create default style (cell style 0)
    StringBuilder sb = new StringBuilder();
    borderStyles(sb, cellStyle);
    defaultFontStyle(cellStyle, sb);
    colorConverter.defaultColorStyles(cellStyle, sb);
    spreadsheet.getState().cellStyleToCSSStyle.put((int) cellStyle.getIndex(), sb.toString());

    // 0 is default style, create all styles indexed from 1 and upwards
    for (short i = 1; i < workbook.getNumCellStyles(); i++) {
        cellStyle = workbook.getCellStyleAt(i);
        addCellStyleCSS(cellStyle);
    }
    reloadActiveSheetColumnRowStyles();
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

private void addCellStyleCSS(CellStyle cellStyle) {

    if (cellStyle.getIndex() == 0) {
        // default cell style, do not change.
        return;/*from w w w  .ja  v a 2 s . c o m*/
    }

    StringBuilder sb = new StringBuilder();

    fontStyle(sb, cellStyle);
    colorConverter.colorStyles(cellStyle, sb);
    borderStyles(sb, cellStyle);
    if (cellStyle.getAlignment() != defaultTextAlign) {
        styleOut(sb, "text-align", cellStyle.getAlignment(), ALIGN);
        // TODO For correct overflow, rtl should be used for right align
        // if (cellStyle.getAlignment() == ALIGN_RIGHT) {
        // sb.append("direction:rtl;");
        // }
    }

    // excel default is bottom, so that is what we have in the CSS base
    // files.
    // TODO This only works on modern (10+) IE.
    styleOut(sb, "justify-content", cellStyle.getVerticalAlignment(), VERTICAL_ALIGN);

    if (cellStyle.getWrapText()) { // default is to overflow
        sb.append("overflow:hidden;white-space:normal;");
    }

    if (cellStyle.getIndention() > 0) {
        sb.append("padding-left: " + cellStyle.getIndention() + "em;");
    }

    spreadsheet.getState().cellStyleToCSSStyle.put((int) cellStyle.getIndex(), sb.toString());
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

private void doCellCustomStyling(final Cell cell) {
    CellStyle cellStyle = cell.getCellStyle();
    final Integer key = (int) cellStyle.getIndex();
    if (key == 0) { // default style
        return;//from  www  .j a  va 2 s . com
    }

    // merged regions have their borders in edge cells that are "invisible"
    // inside the region -> right and bottom cells need to be transfered to
    // the actual merged cell
    final int columnIndex = cell.getColumnIndex();
    final int rowIndex = cell.getRowIndex();
    MergedRegion region = spreadsheet.mergedRegionContainer.getMergedRegion((columnIndex + 1), (rowIndex + 1));
    if (region != null) {
        final String borderRight = getBorderRightStyle(cellStyle);
        final String borderBottom = getBorderBottomStyle(cellStyle);
        if ((borderRight != null && !borderRight.isEmpty())
                || (borderBottom != null && !borderBottom.isEmpty())) {
            StringBuilder sb = new StringBuilder(".col");
            sb.append(region.col1);
            sb.append(".row");
            sb.append(region.row1);
            final String cssKey = sb.toString();
            final String currentBorders = mergedCellBorders.get(cssKey);
            StringBuilder style;
            if (currentBorders != null && !currentBorders.isEmpty()) {
                style = new StringBuilder(currentBorders);
            } else {
                style = new StringBuilder();
            }
            if (borderRight != null && !borderRight.isEmpty()
                    && (currentBorders == null || !currentBorders.contains("border-right"))) {
                style.append(borderRight);
            }
            if (borderBottom != null && !borderBottom.isEmpty()
                    && (currentBorders == null || !currentBorders.contains("border-bottom"))) {
                style.append(borderBottom);
            }
            final String newBorders = style.toString();
            if (!newBorders.isEmpty()) {
                mergedCellBorders.put(cssKey, newBorders);
            }
        }

    }

    // only take transfered borders into account on the (possible) merged
    // regions edges
    if (region == null || region.col1 == (columnIndex + 1) || region.col2 == (columnIndex + 1)
            || region.row1 == (rowIndex + 1) || region.row2 == (rowIndex + 1)) {

        if (shiftedBorderLeftStyles.containsKey(key)) {
            // need to add the border right style to previous cell on
            // left, which might be a merged cell
            if (columnIndex > 0) {
                int row, col;

                MergedRegion previousRegion = spreadsheet.mergedRegionContainer.getMergedRegion(columnIndex,
                        rowIndex + 1);
                if (previousRegion != null) {
                    col = previousRegion.col1;
                    row = previousRegion.row1;
                } else {
                    col = columnIndex;
                    row = rowIndex + 1;
                }
                insertMapEntryIfNeeded(shiftedBorderLeftStylesMap, key, row, col);
            }
        }
        if (shiftedBorderTopStyles.containsKey(key)) {
            // need to add the border bottom style to cell on previous
            // row, which might be a merged cell
            if (rowIndex > 0) {
                int row, col;
                MergedRegion previousRegion = spreadsheet.mergedRegionContainer.getMergedRegion(columnIndex + 1,
                        rowIndex);

                if (previousRegion != null) {
                    col = previousRegion.col1;
                    row = previousRegion.row1;
                } else {
                    col = columnIndex + 1;
                    row = rowIndex;
                }
                insertMapEntryIfNeeded(shiftedBorderTopStylesMap, key, row, col);

            }
        }

    }
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

private void defaultFontStyle(CellStyle cellStyle, StringBuilder sb) {
    if (cellStyle.getIndex() == 0) {
        defaultFont = spreadsheet.getWorkbook().getFontAt(cellStyle.getFontIndex());
        defaultFontFamily = styleFontFamily(defaultFont);
        sb.append(defaultFontFamily);/* w  w w . java 2  s .  co  m*/
        if (defaultFont.getBoldweight() != Font.BOLDWEIGHT_NORMAL) {
            sb.append("font-weight:");
            sb.append(defaultFont.getBoldweight());
            sb.append(";");
        }
        if (defaultFont.getItalic()) {
            sb.append("font-style:italic;");
        }
        defaultFontHeightInPoints = defaultFont.getFontHeightInPoints();
        sb.append("font-size:");
        sb.append(defaultFontHeightInPoints);
        sb.append("pt;");
        if (defaultFont.getUnderline() != Font.U_NONE) {
            sb.append("text-decoration:underline;");
        } else if (defaultFont.getStrikeout()) {
            sb.append("text-decoration:overline;");
        }
    }
}