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.compassplus.gui.MainForm.java

private boolean analyzeCell(Workbook wb, Sheet sheet, Row row, Cell cell, ScriptEngine engine,
        Bindings bindings) {//from w  ww.  j  ava 2s. c  o m
    try {
        String formula = cell.getCellFormula();
        cell.setCellFormula(formula);
    } catch (Exception e) {
    }
    try {
        String expr = cell.getStringCellValue();
        short type = 0;
        if (expr.contains("__REMOVE")) {
            type = __REMOVE;
        } else if (expr.contains("__INSERT")) {
            type = __INSERT;
        }
        if (type > 0) {
            try {
                expr = expr.substring(expr.indexOf("(") + 1);
                expr = expr.substring(0, expr.lastIndexOf(")"));
                expr = expr.replaceAll("\\s", "");

                if (type == __REMOVE) {
                    cell.setCellValue("");
                    Object val = null;
                    val = engine.eval(expr, bindings);
                    if (val instanceof Boolean) {
                        return (Boolean) val;
                    } else {
                        throw new Exception("result is not boolean");
                    }
                } else if (type == __INSERT) {
                    cell.setCellValue("");
                    Object val = null;
                    val = engine.eval(expr, bindings);

                    if (!(val instanceof String && ((String) val).equals(""))) {
                        CellStyle cs = wb.createCellStyle();
                        CellStyle csT = cell.getCellStyle();
                        cs.cloneStyleFrom(csT);
                        String format = (getCurrentProposalForm().getProposal().getCurrency()
                                .getSymbol() != null ? "\""
                                        + getCurrentProposalForm().getProposal().getCurrency().getSymbol()
                                        + "\" " : "")
                                + "#,##0"
                                + (getCurrentProposalForm().getProposal().getCurrency().getSymbol() == null
                                        ? " \"" + getCurrentProposalForm().getProposal().getCurrency().getName()
                                                + "\""
                                        : "");
                        if (expr.contains("$PRICE") || expr.contains("$MAN-DAY-RATE")) {
                            cs.setDataFormat(sheet.getWorkbook().createDataFormat().getFormat(format));
                        } else if (expr.contains("$SUPPORT_RATE")) {
                            cs.setDataFormat(sheet.getWorkbook().createDataFormat().getFormat("0%;-0%"));
                        } else if (expr.contains("$VALUE")) {
                            cs.setDataFormat(sheet.getWorkbook().createDataFormat().getFormat("#,##0"));
                        }
                        cell.setCellStyle(cs);
                    }
                    if (val instanceof Boolean) {
                        cell.setCellValue((Boolean) val);
                    } else if (val instanceof Number) {
                        cell.setCellValue(((Number) val).doubleValue());
                    } else if (val instanceof String) {
                        cell.setCellValue((String) val);
                    } else {
                        throw new Exception("result type is unknown");
                    }
                }

            } catch (Exception e) {
                log.error("Bad" + (type == __REMOVE ? " __REMOVE" : (type == __INSERT ? " __INSERT" : ""))
                        + " expression: " + expr);
                cell.setCellValue("");
            }
        }
    } catch (Exception e) {
    }
    return false;
}

From source file:com.dituiba.excel.BaseExcelService.java

License:Apache License

/**
 * ?/*from  w  w  w . j a v a2  s. c  om*/
 * @param cell
 */
public static void setErrorStyle(Cell cell) {
    if (cell != null) {
        CellStyle newstyle = cell.getSheet().getWorkbook().createCellStyle();
        CellStyle style = cell.getCellStyle();
        if (style != null) {
            newstyle.cloneStyleFrom(style);
        }
        newstyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        newstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(newstyle);
    }
}

From source file:com.dituiba.excel.DefaultOutputAdapter.java

License:Apache License

/**
 * ?//from   ww  w . jav a  2 s. c o m
 *
 * @param fieldValue
 * @param fieldName
 * @return
 * @throws AdapterException
 */
public void outputDateAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:outputDateAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    Date date = null;
    if (fieldValue == null) {
        log.debug("fieldValue is null return");
        cell.setCellValue("");
        return;
    } else if (fieldValue instanceof Date) {
        log.debug("fieldValue instanceof Date ");
        date = (Date) fieldValue;
    } else if (fieldValue instanceof String) {
        log.debug("fieldValue instanceof String ");
        InputDateConfig config = dataBean.getInputConfig(fieldName);
        try {
            date = DateUtil.formatToDate((String) fieldValue, config.format());
        } catch (ParseException e) {
            throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
        }
    } else if (fieldValue instanceof Long) {
        log.debug("fieldValue instanceof Long ");
        date = new Date((Long) fieldValue);
    } else {
        throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
    }
    Workbook workbook = cell.getSheet().getWorkbook();
    OutputDateConfig outputConfig = dataBean.getOutputConfig(fieldName);
    CellStyle cellStyle = cell.getCellStyle();
    if (cellStyle == null)
        cellStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(outputConfig.format()));
    cell.setCellStyle(cellStyle);
    cell.setCellValue(date);
}

From source file:com.dituiba.excel.ExcelUtility.java

License:Apache License

public static void copyCell(Cell srcCell, Cell distCell) {
    distCell.setCellStyle(srcCell.getCellStyle());
    if (srcCell.getCellComment() != null) {
        distCell.setCellComment(srcCell.getCellComment());
    }/*ww  w  .ja v  a 2 s  . com*/
    int srcCellType = srcCell.getCellType();
    distCell.setCellType(srcCellType);

    if (srcCellType == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(srcCell)) {
            distCell.setCellValue(srcCell.getDateCellValue());
        } else {
            distCell.setCellValue(srcCell.getNumericCellValue());
        }
    } else if (srcCellType == Cell.CELL_TYPE_STRING) {
        distCell.setCellValue(srcCell.getRichStringCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_BLANK) {
        //nothing
    } else if (srcCellType == Cell.CELL_TYPE_BOOLEAN) {
        distCell.setCellValue(srcCell.getBooleanCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_ERROR) {
        distCell.setCellErrorValue(srcCell.getErrorCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_FORMULA) {
        distCell.setCellFormula(srcCell.getCellFormula());
    } else {
        //nothing
    }
}

From source file:com.dituiba.excel.ExportTableService.java

License:Apache License

public void doExport() {
    Collection<CellBean> cellBeans = tableBean.getCellBeans();
    if (ObjectHelper.isNotEmpty(cellBeans)) {
        for (CellBean cellBean : cellBeans) {
            if (cellBean.getXSize() > 1 || cellBean.getYSize() > 1) {
                log.debug("??{}", JsonUtil.toJSON(cellBean));
                CellRangeAddress range = new CellRangeAddress(cellBean.getRowIndex(),
                        cellBean.getRowIndex() + cellBean.getYSize() - 1, cellBean.getColumnIndex(),
                        cellBean.getColumnIndex() + cellBean.getXSize() - 1);
                sheet.addMergedRegion(range);
            }/*from ww w.jav  a 2s  .  c o m*/
            log.debug("set row:{},column:{},content:{}", cellBean.getRowIndex(), cellBean.getColumnIndex(),
                    cellBean.getContent());
            Cell cell = sheet.getRow(cellBean.getRowIndex()).getCell(cellBean.getColumnIndex());
            cell.setCellValue(cellBean.getContent());
            CellStyle cellStyle = cell.getCellStyle();
            if (cellStyle == null) {
                cellStyle = sheet.getWorkbook().createCellStyle();
            }
            if (cellBean.isAlignCenter()) {
                cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//
            }
            if (cellBean.isVerticalCenter()) {
                cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//
            }
            cellStyle.setWrapText(cellBean.isWrapText());
            cell.setCellStyle(cellStyle);
        }
    }
}

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

License:Apache License

/**
 * ??// w w w.  java 2  s. com
 *
 * @param sheet
 * @param columnIndex
 * @param style
 * @return
 */
public static boolean setColumnStyle(Sheet sheet, short columnIndex, int rowFirstIndex, int rowLastIndex,
        CellStyle style) {
    if (sheet == null)
        return false;

    int rowNum = sheet.getLastRowNum();
    CellStyle newCellStyle = sheet.getWorkbook().createCellStyle();
    // ??
    if (rowFirstIndex < rowLastIndex) {
        int temp = rowFirstIndex;
        rowFirstIndex = rowLastIndex;
        rowLastIndex = temp;
    }
    // TODO 
    if (rowNum < rowFirstIndex) {// ?
        return false;
    }
    // 
    for (int i = rowFirstIndex; i <= rowNum; i++) {
        Row row = sheet.getRow(i);
        if (row == null)
            return false;
        Cell cell = row.getCell(columnIndex);
        if (cell == null)
            return false;
        newCellStyle.cloneStyleFrom(cell.getCellStyle());// ??
        newCellStyle.cloneStyleFrom(style); // ??
        cell.setCellStyle(newCellStyle);

    }

    return true;
}

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

License:Apache License

/**
 * ????//  w  ww .j  a  va2s.com
 *
 * @param sheet
 * @param rowIndex
 * @param style
 * @return
 */
public static boolean setRowStyle(Sheet sheet, int rowIndex, CellStyle style) {
    if (sheet != null) {
        Row row = sheet.getRow(rowIndex);
        if (row != null) {
            short firstColumnIndex = row.getFirstCellNum();
            short lastColumnIndex = row.getLastCellNum();
            for (short colunmIndex = firstColumnIndex; colunmIndex < lastColumnIndex; colunmIndex++) {
                CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
                Cell cell = row.getCell(colunmIndex);
                if (cell != null) {
                    cellStyle.cloneStyleFrom(cell.getCellStyle());
                    cellStyle.cloneStyleFrom(style);
                    cell.setCellStyle(cellStyle);
                }
            }
        }
    }

    return true;
}

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

License:Apache License

/**
 * ???/*from   w  w w .j av  a  2 s.  c o m*/
 *
 * @param sheet
 * @param rowIndex
 * @param columnIndex
 * @param style
 * @return
 */
public static boolean setCellStyle(Sheet sheet, int rowIndex, int columnIndex, CellStyle style) {
    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.fjn.helper.common.io.file.office.excel.ListExcelSheetEditor.java

License:Apache License

/**
 * ??//from  w  w w .j  a  va  2  s. c o  m
 * @param sheet
 * @param columnIndex
 * @param style
 * @return
 */
public boolean setColumnStyle(int columnIndex, int rowFirstIndex, int rowLastIndex, CellStyle style) {
    Sheet sheet = excelSheet.sheet;
    if (sheet == null)
        return false;

    int rowNum = sheet.getLastRowNum();
    CellStyle newCellStyle = sheet.getWorkbook().createCellStyle();
    // ??
    if (rowFirstIndex > rowLastIndex) {
        int temp = rowFirstIndex;
        rowFirstIndex = rowLastIndex;
        rowLastIndex = temp;
    }
    if (rowNum < rowFirstIndex) {// ?
        return false;
    }
    // 
    for (int i = rowFirstIndex; i <= rowNum; i++) {
        Row row = sheet.getRow(i);
        if (row == null)
            return false;
        Cell cell = row.getCell(columnIndex);
        if (cell == null)
            return false;
        newCellStyle.cloneStyleFrom(cell.getCellStyle());// ??
        newCellStyle.cloneStyleFrom(style); //   ??
        cell.setCellStyle(newCellStyle);

    }

    return true;
}

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

License:Apache License

/**
 * ????/*  w w w.java  2 s  .  c o  m*/
 * @param sheet
 * @param rowIndex
 * @param style
 * @return
 */
public boolean setRowStyle(int rowIndex, CellStyle style) {
    Sheet sheet = excelSheet.sheet;
    if (sheet != null) {
        Row row = sheet.getRow(rowIndex);
        if (row != null) {
            short firstColumnIndex = row.getFirstCellNum();
            short lastColumnIndex = row.getLastCellNum();
            for (short colunmIndex = firstColumnIndex; colunmIndex < lastColumnIndex; colunmIndex++) {
                CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
                Cell cell = row.getCell(colunmIndex);
                if (cell != null) {
                    cellStyle.cloneStyleFrom(cell.getCellStyle());
                    cellStyle.cloneStyleFrom(style);
                    cell.setCellStyle(cellStyle);
                }
            }
        }
    }

    return true;
}