/** * Returns the format string, eg $##.##, for the given number format index. * * @param formatIndex the format index * * @return the format string */ public String getFormatString(int formatIndex) { String format = null; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) { FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex)); if (tfr == null) { logger.log( POILogger.ERROR, "Requested format at index " + formatIndex + ", but it wasn't found"); } else { format = tfr.getFormatString(); } } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
public String getFormatString(int formatIndex) { if (formatIndex < 0) { return null; } String format; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats() || customFormats.get(formatIndex) != null) { format = customFormats.get(formatIndex).getFormatString(); } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
/** * Returns the format string, eg $##.##, for the given number format index. */ public String getFormatString(int formatIndex) { String format = null; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) { FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex)); if (tfr == null) { System.err.println("Requested format at index " + formatIndex + ", but it wasn't found"); } else { format = tfr.getFormatString(); } } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {// 用到 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); return cellStyle; }
private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {//用到 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); return cellStyle; }
@Override public String getDataFormatString() { int formatIndex = getDataFormat(); if (formatIndex < 0) { return null; } String format; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats() || formats.get(formatIndex) != null) { format = formats.get(formatIndex).getFormatString(); } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
/** * Returns the format string, eg $##.##, for the given number format index. */ public String getFormatString(int formatIndex) { String format = null; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) { FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex)); if (tfr == null) { logger.log( POILogger.ERROR, "Requested format at index " + formatIndex + ", but it wasn't found"); } else { format = tfr.getFormatString(); } } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
/** * Returns the format string, eg $##.##, for the given number format index. * * @param formatIndex the format index * * @return the format string */ public String getFormatString(int formatIndex) { String format = null; if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) { FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex)); if (tfr == null) { logger.log( POILogger.ERROR, "Requested format at index " + formatIndex + ", but it wasn't found"); } else { format = tfr.getFormatString(); } } else { format = HSSFDataFormat.getBuiltinFormat((short) formatIndex); } return format; }
protected void createFormats() { timeFormatCellStyle = wb.createCellStyle(); timeFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm")); dateFormatCellStyle = wb.createCellStyle(); dateFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); dateTimeFormatCellStyle = wb.createCellStyle(); dateTimeFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); integerFormatCellStyle = wb.createCellStyle(); integerFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); DataFormat format = wb.createDataFormat(); doubleFormatCellStyle = wb.createCellStyle(); doubleFormatCellStyle.setDataFormat(format.getFormat("#,##0.################")); }
private static final void writeCell(final Cell cell, final Object value) { if (value instanceof Number) { cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Number.class.cast(value).doubleValue()); } else if (value instanceof Date) { final CellStyle style = cell.getSheet().getWorkbook().createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); cell.setCellStyle(style); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Date.class.cast(value)); } else if (value instanceof Boolean) { cell.setCellType(Cell.CELL_TYPE_BOOLEAN); } else { cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(value != null ? String.valueOf(value) : ""); } }
private static final void writeCell(final Cell cell, final Object value) { if (value instanceof Number) { cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Number.class.cast(value).doubleValue()); } else if (value instanceof Date) { final CellStyle style = cell.getSheet().getWorkbook().createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); cell.setCellStyle(style); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Date.class.cast(value)); } else if (value instanceof Boolean) { cell.setCellType(Cell.CELL_TYPE_BOOLEAN); } else { cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(value != null ? String.valueOf(value) : ""); } } }
/** * We cache the styles; they are expensive to construct. * @param properties props for this run */ public void initCellStyles(TableProperties properties) { // Integer HSSFCellStyle style = getNewCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_INTEGER))); this.cellStyles.put(STYLE_INTEGER, style); // NUMBER style = getNewCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_NUMBER))); this.cellStyles.put(STYLE_NUMBER, style); // Date style = getNewCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_DATE))); style.setAlignment(HorizontalAlignment.RIGHT); this.cellStyles.put(STYLE_DATE, style); // Long text style = getNewCellStyle(); // http://jakarta.apache.org/poi/hssf/quick-guide.html#NewLinesInCells style.setWrapText(true); this.cellStyles.put(STYLE_LONGSTRING, style); // Regular text this.cellStyles.put(STYLE_STRING, getNewCellStyle()); this.wrapAt = Integer.valueOf(properties.getProperty(ExcelUtils.EXCEL_WRAPAT)); }
public static void main(String[] args) throws IOException { try (HSSFWorkbook wb = new HSSFWorkbook()) { HSSFSheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. HSSFRow row = sheet.createRow(0); // Create a cell and put a date value in it. The first cell is not styled as a date. HSSFCell cell = row.createCell(0); cell.setCellValue(new Date()); // we style the second cell as a date (and time). It is important to create a new cell style from the workbook // otherwise you can end up modifying the built in style and effecting not only this cell but other cells. HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); cell = row.createCell(1); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); // Write the output to a file try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) { wb.write(fileOut); } } } }
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式 SimpleDateFormat sdf = null; if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) { sdf = new SimpleDateFormat("HH:mm"); } else {// 日期
cellStyle = workbook.createCellStyle(); format = StringUtils.isBlank(format) ? DEFAULT_NUM_FORMAT : format; cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(format)); cell.setCellStyle(cellStyle); if (object instanceof Integer) {
/** * Instantiates a new Poi cell style. * * @param workBook the work book */ public PoiCellStyle(PoiWorkBook workBook) { this.stringCs = this.getDefaultExcelCellStyle(workBook); this.numberCs = this.getDefaultExcelCellStyle(workBook); this.numberCs.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); this.numberCs.setAlignment(HorizontalAlignment.RIGHT); this.dateCs = this.getDefaultExcelCellStyle(workBook); this.dateCs.setDataFormat(DATE_FORMAT_VALUE); }
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
f2.setBold(true); cs.setFont(f); cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cs2.setBorderBottom(BorderStyle.THIN); cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));