List of usage examples for org.apache.poi.hssf.usermodel HSSFCreationHelper createDataFormat
@Override
public HSSFDataFormat createDataFormat()
From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java
License:Apache License
private void initStyles(HSSFWorkbook wb) { // cache styles used to write text into cells HSSFCellStyle style = wb.createCellStyle(); HSSFFont font = wb.createFont();/*from w w w .j a v a 2s . c o m*/ font.setBold(true); style.setFont(font); styles.put(STYLE_BOLD, style); style = wb.createCellStyle(); font = wb.createFont(); font.setItalic(true); style.setFont(font); styles.put(STYLE_ITALIC, style); style = wb.createCellStyle(); font = wb.createFont(); font.setItalic(true); font.setColor(Font.COLOR_RED); style.setFont(font); styles.put(STYLE_REVIEW, style); style = wb.createCellStyle(); style.setFillPattern(FillPatternType.FINE_DOTS); style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.BLUE_GREY.getIndex()); style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE_GREY.getIndex()); styles.put(STYLE_MISSING, style); style = wb.createCellStyle(); style.setFillPattern(FillPatternType.FINE_DOTS); style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.BLUE_GREY.getIndex()); style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE_GREY.getIndex()); style.setFont(font); styles.put(STYLE_MISSING_REVIEW, style); style = wb.createCellStyle(); HSSFCreationHelper createHelper = wb.getCreationHelper(); style.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-dd-mm hh:mm")); styles.put(STYLE_DATETIME, style); }
From source file:net.vpc.app.vainruling.core.web.jsf.Vr.java
public void postProcessDataExporterXLS(Object document) { HSSFWorkbook book = (HSSFWorkbook) document; HSSFSheet sheet = book.getSheetAt(0); HSSFRow header = sheet.getRow(0);/* www .j ava 2 s .c o m*/ int rowCount = sheet.getPhysicalNumberOfRows(); HSSFCellStyle headerCellStyle = book.createCellStyle(); headerCellStyle.setFillForegroundColor(HSSFColor.AQUA.index); headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); HSSFCreationHelper creationHelper = book.getCreationHelper(); for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) { HSSFCell cell = header.getCell(i); cell.setCellStyle(headerCellStyle); } HSSFCellStyle intStyle = book.createCellStyle(); intStyle.setDataFormat((short) 1); HSSFCellStyle decStyle = book.createCellStyle(); decStyle.setDataFormat((short) 2); HSSFCellStyle dollarStyle = book.createCellStyle(); dollarStyle.setDataFormat((short) 5); int maxColumn = -1; Map<String, HSSFCellStyle> datFormats = new HashMap<>(); for (int rowInd = 1; rowInd < rowCount; rowInd++) { HSSFRow row = sheet.getRow(rowInd); int colCount = row.getPhysicalNumberOfCells(); if (maxColumn < colCount) { maxColumn = colCount; } for (int cellInd = 0; cellInd < colCount; cellInd++) { HSSFCell cell = row.getCell(cellInd); String strVal = cell.getStringCellValue(); if (strVal.startsWith("$")) { //do nothing } else { if (strVal.startsWith("'")) { strVal = strVal.substring(1); } if (PlatformUtils.isDouble(strVal)) { cell.setCellType(HSSFCell.CELL_TYPE_BLANK); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); if (PlatformUtils.isInteger(strVal)) { int intVal = Integer.valueOf(strVal.trim()); cell.setCellStyle(intStyle); cell.setCellValue(intVal); } else if (PlatformUtils.isDouble(strVal)) { double dblVal = Double.valueOf(strVal.trim()); cell.setCellStyle(decStyle); cell.setCellValue(dblVal); } } else { boolean isDate = false; for (String dteFormat : new String[] { "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "HH:mm" }) { if (PlatformUtils.isDate(strVal, dteFormat)) { HSSFCellStyle dateStyle = datFormats.get(dteFormat.trim()); if (dateStyle == null) { dateStyle = book.createCellStyle(); dateStyle.setDataFormat(creationHelper.createDataFormat().getFormat(dteFormat)); datFormats.put(dteFormat, dateStyle); } cell.setCellStyle(dateStyle); try { cell.setCellValue(new SimpleDateFormat(dteFormat).parse(strVal)); } catch (ParseException e) { // } isDate = true; break; } } } } } } if (maxColumn >= 0) { for (int cellInd = 0; cellInd < maxColumn; cellInd++) { sheet.autoSizeColumn(cellInd); } } }