Set Cell Style
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = Workbook.createSheet("sheet title"); XSSFRow row; XSSFCell cell; int indexRow, indexColumn; row=sheet.createRow(indexRow); cell=row.createCell(indexColumn); CellStyle style = workbook.createCellStyle(); cell.setCellStyle(style);
Wrap Style
CellStyle wrap_style = workbook.createCellStyle(); wrap_style.setWrapText(CellStyle percent_style = workbook.createCellStyle(); percent_style.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));true);
Border Style
XSSFWorkbook workbook = new XSSFWorkbook(); CellStyle border_style = workbook.createCellStyle(); border_style.setBorderLeft(BorderStyle.THIN); border_style.setBorderRight(BorderStyle.THIN); border_style.setBorderTop(BorderStyle.THIN); border_style.setBorderBottom(BorderStyle.THIN);
Bold Style
Font font = workbook.createFont(); font.setBold(true); CellStyle bold_style = workbook.createCellStyle(); bold_style.setFont(font);
Percent Format
CellStyle percent_style = workbook.createCellStyle(); percent_style.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
Merge Cell
sheet.addMergedRegion(new CellRangeAddress(nrowfirst, nrowlast, ncolumnfirst, ncolumnlast));
Formula Cell
cell.setCellType(CellType.FORMULA); cell.setCellFormula("A1+A2");
Looping Cell in Region (Range)
CellRangeAddress region = CellRangeAddress.valueOf("A1:B10"); for (int i = region.getFirstRow(); i < region.getLastRow(); i++) { XSSFRow temprow = sheet.getRow(i); for (int j = region.getFirstColumn(); j <= (region.getLastColumn()); j++) { XSSFCell tempcell = temprow.getCell(j); if (tempcell != null) { tempcell.setCellStyle(cellStyle); } else { tempcell = temprow.createCell(j); tempcell.setCellStyle(cellStyle); } } }
Comments
Post a Comment