Example usage for org.apache.poi.ss.usermodel Cell getCellStyle

List of usage examples for org.apache.poi.ss.usermodel Cell getCellStyle

Introduction

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

Prototype

CellStyle getCellStyle();

Source Link

Document

Return the cell's style.

Usage

From source file:com.fjn.helper.common.io.file.office.excel.ListExcelSheetEditor.java

License:Apache License

/**
 * ???/* www .  j a va 2s . c  o  m*/
 * @param sheet
 * @param rowIndex
 * @param columnIndex
 * @param style
 * @return
 */
public boolean setCellStyle(int rowIndex, int columnIndex, CellStyle style) {
    Sheet sheet = excelSheet.sheet;
    if (sheet == null)
        return false;
    if (rowIndex < 0 || columnIndex <= 0)
        return false;

    Cell cell = sheet.getRow(rowIndex).getCell(columnIndex);
    if (cell == null)
        return false;

    CellStyle newCellStyle = sheet.getWorkbook().createCellStyle();
    newCellStyle.cloneStyleFrom(cell.getCellStyle());
    newCellStyle.cloneStyleFrom(style);
    cell.setCellStyle(newCellStyle);
    return true;
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

/**
 * ???//from  www . jav  a2s  .c  o m
 */
private static void initStyles(SheetTemplate template, Cell cell, String moduleContext) {
    if (null == moduleContext || "".equals(moduleContext))
        return;
    if (!moduleContext.startsWith("&"))
        moduleContext = moduleContext.toLowerCase();
    if (HandlerConstant.DEFAULT_STYLE.equals(moduleContext)) {
        template.defaultStyle = cell.getCellStyle();
        clearCell(cell);
    }
    if (moduleContext.startsWith("&") && moduleContext.length() > 1) {
        template.classifyStyle.put(moduleContext.substring(1), cell.getCellStyle());
        clearCell(cell);
    }
    if (HandlerConstant.APPOINT_LINE_STYLE.equals(moduleContext)) {
        template.appointLineStyle.put(cell.getRowIndex(), cell.getCellStyle());
        clearCell(cell);
    }
    if (HandlerConstant.SINGLE_LINE_STYLE.equals(moduleContext)) {
        template.singleLineStyle = cell.getCellStyle();
        clearCell(cell);
    }
    if (HandlerConstant.DOUBLE_LINE_STYLE.equals(moduleContext)) {
        template.doubleLineStyle = cell.getCellStyle();
        clearCell(cell);
    }
}

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

License:Open Source License

public WrapperCellStyle getStyle(Workbook workbook, Cell poiCell) {
    if (poiCell == null)
        return null;

    final CellStyle cellStyle = poiCell.getCellStyle();
    return this.toWrapperCellStyle(workbook, cellStyle);
}

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

License:Open Source License

public String getStyleName(Cell cell) {
    if (cell == null)
        return null;

    final CellStyle cellStyle = cell.getCellStyle();
    return this.getStyleName(cellStyle);
}

From source file:com.github.xiilei.ecdiff.Processor.java

License:Apache License

public boolean cellComparer(Cell src_cell, Cell dist_cell) {
    if (dist_cell == null) {
        logger.warn("comparer,dist_cell is null");
        return false;
    }//ww  w  .  j  a  v a 2 s . c  om
    if (src_cell == null || !getStringCellValue(dist_cell).trim().equals(getStringCellValue(src_cell))) {
        CellStyle style = dist_cell.getCellStyle();
        style.setFont(this.font);
        dist_cell.setCellStyle(style);
    }
    return true;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.SummaryReportGenerator.java

License:Apache License

/**
 * Creates Leveraging Sheet//w  w  w  . j a  v a2  s.c  om
 */
private void createCostsSheet(Workbook p_workbook, Sheet p_sheet, ReportSearchOptions p_options,
        Map<String, ReportWordCount> p_wordCounts) throws Exception {
    int rowLen = p_sheet.getPhysicalNumberOfRows();
    int colLen = p_sheet.getRow(2).getPhysicalNumberOfCells();
    int wordTotalCol = colLen - 2;
    int row = ROWNUMBER, column = colLen - 1;
    int costCol;
    Map<String, Double> p_ratesMap = null;
    for (int r = 2; r < rowLen + ROWNUMBER; r++) {
        Row theRow = getRow(p_sheet, r);
        theRow.removeCell(getCell(theRow, column));
    }
    p_sheet.removeColumnBreak(column);
    // Rates Columns
    for (int dis = column - 1; column < colLen + dis - 2; column++) {
        Cell cell_From = p_sheet.getRow(row).getCell(column - dis);
        Cell cell_To = getCell(p_sheet.getRow(row), column);
        cell_To.setCellValue(cell_From.getStringCellValue());
        cell_To.setCellStyle(cell_From.getCellStyle());
        p_sheet.setColumnWidth(column, p_sheet.getColumnWidth(column - dis));
        // Adds Rates for Match Type
        for (int rateRow = row + 1; rateRow <= rowLen; rateRow++) {
            String matchType = p_sheet.getRow(ROWNUMBER).getCell(column).getStringCellValue();
            String targetLocale = p_sheet.getRow(rateRow).getCell(0).getStringCellValue();
            double rate = getRate(matchType, targetLocale, p_ratesMap);
            addNumberCell(p_sheet, column, rateRow, rate, getMoneyStyle(p_workbook));
        }
    }

    // Cost Columns Head
    costCol = column;
    p_sheet.setColumnWidth(column, 20 * 256);
    Cell cell_CostWithLeveraging = getCell(getRow(p_sheet, row), column++);
    cell_CostWithLeveraging.setCellValue(bundle.getString("lb_report_costWithLeveraging"));
    cell_CostWithLeveraging.setCellStyle(getHeaderOrangeStyle(p_workbook));

    p_sheet.setColumnWidth(column, 20 * 256);
    Cell cell_CostNoLeveraging = getCell(getRow(p_sheet, row), column++);
    cell_CostNoLeveraging.setCellValue(bundle.getString("lb_report_costNoLeveraging"));
    cell_CostNoLeveraging.setCellStyle(getHeaderOrangeStyle(p_workbook));

    p_sheet.setColumnWidth(column, 15 * 256);
    Cell cell_Savings = getCell(getRow(p_sheet, row), column++);
    cell_Savings.setCellValue(bundle.getString("lb_savings"));
    cell_Savings.setCellStyle(getHeaderOrangeStyle(p_workbook));

    Cell cell_Percent = getCell(getRow(p_sheet, row), column++);
    cell_Percent.setCellValue("%");
    cell_Percent.setCellStyle(getHeaderOrangeStyle(p_workbook));
    // Cost Columns Data
    for (row = ROWNUMBER + 1; row < (rowLen + ROWNUMBER); row++) {
        String leveragingForm = getCostWithLeveraging(1, wordTotalCol - 1, wordTotalCol, (row + 1));
        String noLeveragingForm = getColumnName(wordTotalCol) + (row + 1) + "*"
                + getColumnName(wordTotalCol + 5) + (row + 1);
        String savingForm = getColumnName(costCol + 1) + (row + 1) + "-" + getColumnName(costCol) + (row + 1);
        String percent = getColumnName(costCol + 2) + (row + 1) + "/" + getColumnName(costCol + 1) + (row + 1);

        Row theRow = getRow(p_sheet, row);
        Cell cell_Leveraging = getCell(theRow, costCol);
        cell_Leveraging.setCellFormula(leveragingForm);
        cell_Leveraging.setCellStyle(getMoneyStyle(p_workbook));

        Cell cell_NoLeveraging = getCell(theRow, costCol + 1);
        cell_NoLeveraging.setCellFormula(noLeveragingForm);
        cell_NoLeveraging.setCellStyle(getMoneyStyle(p_workbook));

        Cell cell_Saving = getCell(theRow, costCol + 2);
        cell_Saving.setCellFormula(savingForm);
        cell_Saving.setCellStyle(getMoneyStyle(p_workbook));

        Cell cell_PercentData = getCell(theRow, costCol + 3);
        cell_PercentData.setCellFormula(percent);
        cell_PercentData.setCellStyle(getPercentStyle(p_workbook));
    }

    if (rowLen > 1) {
        row = rowLen + 1;
        column = 1;
        for (; column < colLen - 1; column++) {
            Cell cell_Total = getCell(getRow(p_sheet, row), column);
            cell_Total.setCellFormula(getSumOfColumn(ROWNUMBER + 1, row - 1, column));
            cell_Total.setCellStyle(getHeaderOrangeStyle(p_workbook));
        }
        for (; column < costCol; column++) {
            Cell cell = getCell(getRow(p_sheet, row), column);
            cell.setCellValue("");
            cell.setCellStyle(getHeaderOrangeStyle(p_workbook));
        }

        // Summary Cost Columns
        Cell cell_SumLeveraging = getCell(getRow(p_sheet, row), column);
        cell_SumLeveraging.setCellFormula(getSumOfColumn(ROWNUMBER + 1, row - 1, column++));
        cell_SumLeveraging.setCellStyle(getMoneySumStyle(p_workbook));

        Cell cell_SumNoLeveraging = getCell(getRow(p_sheet, row), column);
        cell_SumNoLeveraging.setCellFormula(getSumOfColumn(ROWNUMBER + 1, row - 1, column++));
        cell_SumNoLeveraging.setCellStyle(getMoneySumStyle(p_workbook));

        Cell cell_SumSaving = getCell(getRow(p_sheet, row), column);
        cell_SumSaving.setCellFormula(getSumOfColumn(ROWNUMBER + 1, row - 1, column++));
        cell_SumSaving.setCellStyle(getMoneySumStyle(p_workbook));

        String percent = getColumnName(column - 1) + (row + 1) + "/" + getColumnName(column - 2) + (row + 1);
        Cell cell_AvgPercent = getCell(getRow(p_sheet, row), column);
        cell_AvgPercent.setCellFormula(percent);
        cell_AvgPercent.setCellStyle(getPercentSumStyle(p_workbook));
    }
}

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

License:Apache License

public void printStyles() {
    ensureOut();/*  w w w  .  j a  va 2  s . co m*/

    // First, copy the base css
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/excelStyle.css")));
        String line;
        while ((line = in.readLine()) != null) {
            out.format("%s%n", line);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Reading standard css", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                //noinspection ThrowFromFinallyBlock
                throw new IllegalStateException("Reading standard css", e);
            }
        }
    }

    // now add css for each used style
    Set<CellStyle> seen = new HashSet<CellStyle>();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            Row row = rows.next();
            for (Cell cell : row) {
                CellStyle style = cell.getCellStyle();
                if (!seen.contains(style)) {
                    printStyle(style);
                    seen.add(style);
                }
            }
        }
    }
}

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

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads(sheet);// w w  w.  j av  a2 s.co m

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        out.format("  <tr>%n");
        out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "&nbsp;";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    //Set the value that is rendered for the cell
                    //also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                    if (content.equals(""))
                        content = "&nbsp;";
                }
            }
            out.format("    <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

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

License:Apache License

private Styles setCellStyle(Cell cell, Styles cellStyles, Styles rowStyles, ResolvedSheetStyles sheetStyles,
        RowPosition rowPosition, ColumnPosition columnPosition, Styles leftStyles, Styles aboveStyles) {

    Styles styles = sheetStyles.resolve(cellStyles, rowStyles, rowPosition, columnPosition, leftStyles,
            aboveStyles);//  ww  w .  ja va  2s .c  om

    adjustAdjacentCellStyle(cell, styles, rowPosition, columnPosition, leftStyles, aboveStyles);

    CellStyle originalStyle = cell.getCellStyle();

    CellStyle finalStyle = composeCellStyle(cell, styles);

    if (finalStyle != originalStyle) {
        cell.setCellStyle(finalStyle);
    }

    return styles;
}

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  ww  .  ja v  a 2 s . c o  m
}