Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

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

Prototype

Cell createCell(int column);

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:com.clican.pluto.dataprocess.engine.processes.ExcelProcessor.java

License:LGPL

@SuppressWarnings("unchecked")
public void writeExcel(ProcessorContext context, ExcelExecBean execBean) throws Exception {
    Workbook book = null;/*  w w  w. ja va  2s.co m*/
    InputStream is = null;
    try {
        is = execBean.getInputStream();
    } catch (FileNotFoundException e) {

    }
    if (is != null) {
        book = WorkbookFactory.create(is);
        is.close();
    } else {
        book = new HSSFWorkbook();
    }
    CreationHelper createHelper = book.getCreationHelper();
    CellStyle dateStyle = book.createCellStyle();
    dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));

    CellStyle numStyle = book.createCellStyle();
    numStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.00000000"));

    CellStyle intNumStyle = book.createCellStyle();
    intNumStyle.setDataFormat(createHelper.createDataFormat().getFormat("0"));

    List<Object> result = context.getAttribute(execBean.getParamName());
    String[] columns = execBean.getColumns();
    if (execBean.getColumns() != null) {
        columns = execBean.getColumns();
    } else {
        columns = ((List<String>) context.getAttribute(execBean.getColumnsVarName())).toArray(new String[] {});
    }
    String sheetName;
    if (StringUtils.isNotEmpty(execBean.getSheetName())) {
        sheetName = execBean.getSheetName();
    } else {
        sheetName = context.getAttribute(execBean.getSheetVarName()).toString();
    }
    // int number = book.getNumberOfSheets();
    Sheet sheet = book.createSheet(sheetName);
    int rowNum = 0;
    Row firstRow = sheet.createRow(rowNum++);
    for (int i = 0; i < columns.length; i++) {
        Cell cell = firstRow.createCell(i);
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(columns[i]);
    }

    for (int i = 0; i < result.size(); i++) {
        Object row = result.get(i);
        Row dataRow = sheet.createRow(rowNum++);

        for (int j = 0; j < columns.length; j++) {
            Object obj = PropertyUtils.getNestedProperty(row, columns[j]);
            Cell cell = dataRow.createCell(j);
            if (obj == null) {
                cell.setCellType(Cell.CELL_TYPE_BLANK);
            } else {
                if (obj instanceof String) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue(obj.toString());
                } else if (obj instanceof Date) {
                    cell.setCellValue((Date) obj);
                    cell.setCellStyle(dateStyle);
                } else if (obj instanceof Integer || obj instanceof Long) {
                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(intNumStyle);
                    cell.setCellValue(new Double(obj.toString()));
                } else if (obj instanceof Number) {
                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(numStyle);
                    cell.setCellValue(new Double(obj.toString()));
                } else {
                    throw new DataProcessException("??Excel?");
                }
            }
        }
    }

    OutputStream os = null;
    try {
        os = execBean.getOutputStream();
        book.write(os);
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:com.cms.utils.ExportExcell.java

/**
 * Tao cac sheet. Chua export ra file excell
 *
 * @param sheetName//  w  ww. j a v a  2 s  .  c o m
 * @param lstData
 * @param formatExcell
 * @param key
 */
public void buildSheet(String sheetName, List<?> lstData, List<FormatExcell> formatExcell, String key) {
    Sheet sheetTmp = wb.createSheet(sheetName);
    FormatExcell item;
    Row rowheader = sheetTmp.createRow(0);
    ExportExcellCelltype customCellType = new ExportExcellCelltype(wb);

    Cell cellStt = rowheader.createCell(0);
    cellStt.setCellValue(BundleUtils.getString("STT"));
    cellStt.setCellStyle(customCellType.cellTypeColumnStt());
    for (int i = 0; i < formatExcell.size(); i++) {
        item = formatExcell.get(i);
        Cell cell = rowheader.createCell(i + 1);
        cell.setCellValue(getColumnTitle(item.getCellColumn(), key));
        cell.setCellStyle(customCellType.cellTypeHeader(false));
        sheetTmp.setColumnWidth(i + 1, item.getColumnWidth());
        sheetTmp.setColumnHidden(i + 1, item.getColumnHiden());
        if (item.getHeaderCommand() != null) {
            cell.setCellComment(getcellComment(sheetTmp, item, cell));
        }
        setFormatCell(customCellType, item);
    }
    if (DataUtil.isListNullOrEmpty(formatExcell) || DataUtil.isListNullOrEmpty(lstData)) {
        return;
    }
    buildContentSheet(sheetTmp, customCellType, lstData, formatExcell);
}

From source file:com.cms.utils.ExportExcell.java

private void buildContentSheet(Sheet sheetTmp, ExportExcellCelltype customCellType, List<?> lstData,
        List<FormatExcell> formatExcell) {

    int i = 1;/*from w  w  w .  j  av a 2 s  .  com*/
    for (Object itemData : lstData) {

        Row rowData = sheetTmp.createRow(i);
        Cell cellStt = rowData.createCell(0);
        cellStt.setCellValue(i);
        cellStt.setCellStyle(customCellType.cellTypeAlignCenter(false));
        i++;
        createCell(itemData, rowData, formatExcell);
    }
}

From source file:com.cms.utils.ExportExcell.java

public void buildSheet(List<?> lstData, List<FormatExcell> formatExcell, String key) {
    FormatExcell item;//from  w  ww. j  av  a2 s. c o m

    Row rowheader = sh.createRow(0);
    customCellType = new ExportExcellCelltype(wb);

    Cell cellStt = rowheader.createCell(0);
    cellStt.setCellValue(BundleUtils.getString("STT"));
    cellStt.setCellStyle(customCellType.cellTypeColumnStt());
    for (int i = 0; i < formatExcell.size(); i++) {
        item = formatExcell.get(i);
        Cell cell = rowheader.createCell(i + 1);
        cell.setCellValue(getColumnTitle(item.getCellColumn(), key));
        cell.setCellStyle(customCellType.cellTypeHeader(false));
        sh.setColumnWidth(i + 1, item.getColumnWidth());
        sh.setColumnHidden(i + 1, item.getColumnHiden());
        if (item.getHeaderCommand() != null) {
            cell.setCellComment(getcellComment(item, cell));
        }
        setFormatCell(item);
    }
    if (DataUtil.isListNullOrEmpty(formatExcell) || DataUtil.isListNullOrEmpty(lstData)) {
        return;
    }
    buildContentSheet(lstData, formatExcell);

}

From source file:com.cms.utils.ExportExcell.java

private void buildContentSheet(List<?> lstData, List<FormatExcell> formatExcell) {

    int i = 1;/*from   w  w  w.j a v  a  2  s  . c o m*/
    for (Object itemData : lstData) {

        Row rowData = sh.createRow(i);
        Cell cellStt = rowData.createCell(0);
        cellStt.setCellValue(i);
        cellStt.setCellStyle(customCellType.cellTypeAlignCenter(false));
        i++;
        createCell(itemData, rowData, formatExcell);

    }
    try {
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
        //            wb.dispose();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.cms.utils.ExportExcell.java

public void createCell(Object itemData, Row rowData, List<FormatExcell> formatExcell) {
    int i = 1;/*from   w  w  w  . j  ava 2 s .c  o  m*/
    try {
        if (!flag) {
            getListMethod(formatExcell, itemData);
        }
        String value = "";
        Double dValue = 0D;
        for (FormatExcell item : formatExcell) {
            Cell cell = rowData.createCell(i);

            cell.setCellStyle(item.getCellStyle());
            if (item.isColumnView()) {
                if (item.getCellType() != null) {
                    if (item.getCellType().equals(FormatExcell.DOUBLE_TYPE)) {
                        if (item.getGetterColumn().invoke(itemData) != null) {
                            dValue = Double.parseDouble((String) item.getGetterColumn().invoke(itemData));
                        }
                        cell.setCellValue(dValue);
                    }
                } else {
                    value = (String) item.getGetterColumn().invoke(itemData);
                    if (DataUtil.isStringNullOrEmpty(value)) {
                        value = "";
                    }
                    cell.setCellValue(value);
                }
            }
            i++;
        }
    } catch (NoSuchMethodException ex) {
        Logger.getLogger(ExportExcell.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(ExportExcell.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalArgumentException ex) {
        Logger.getLogger(ExportExcell.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException ex) {
        Logger.getLogger(ExportExcell.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        flag = false;
    }
}

From source file:com.company.et.service.XlsService.java

private static void createConstStringCells(Workbook wb, Sheet s, String text, Row rowFirst,
        int coordinateColumn, int mergedRegionCoordinatRow1, int mergedRegionCoordinatRow2,
        int mergedRegionCoordinatColumn1, int mergedRegionCoordinatColumn2) {

    s.addMergedRegion(new CellRangeAddress(mergedRegionCoordinatRow1, mergedRegionCoordinatRow2,
            mergedRegionCoordinatColumn1, mergedRegionCoordinatColumn2));
    Cell firstCell = rowFirst.createCell(coordinateColumn);
    firstCell.setCellValue(text);//  w  ww  .  j  a v a  2s  .c  o  m
    setCellStyle(wb, s, firstCell);

}

From source file:com.company.et.service.XlsService.java

private static void createCellOfDouble(Workbook wb, Sheet s, Row row, int column, Double get) {
    Cell cell = row.createCell(column);
    cell.setCellValue(get);//from  w  ww  .ja  v  a  2 s  . c om
    setCellStyle(wb, s, cell);
}

From source file:com.company.et.service.XlsService.java

private static void createFullTasksCell(Workbook wb, Sheet s, Row row, int column) {
    Cell cell = row.createCell(column);
    cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
    cell.setCellFormula("C" + (row.getRowNum() + 1) + "+ F" + (row.getRowNum() + 1) + "+ I"
            + (row.getRowNum() + 1) + "+ L" + (row.getRowNum() + 1));
    setCellStyle(wb, s, cell);// w w  w  . j a v  a  2s  .  co m
}

From source file:com.company.et.service.XlsService.java

private static void createFullTasksActualCell(Workbook wb, Sheet s, Row row, int column) {
    Cell cell = row.createCell(column);
    cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
    cell.setCellFormula("B" + (row.getRowNum() + 1) + "+ E" + (row.getRowNum() + 1) + "+ H"
            + (row.getRowNum() + 1) + "+ K" + (row.getRowNum() + 1));
    setCellStyle(wb, s, cell);//from w w w.  java 2s  .  com
}