public MyXlsContentProvider(final ExportWorkbook workbook) { super(new MyXlsExportContext(), workbook); defaultFormatMap.put(DateHolder.class, new CellFormat("YYYY-MM-DD").setAutoDatePrecision(true)); // format unused. defaultFormatMap.put(DayHolder.class, new CellFormat(DateFormats.getExcelFormatString(DateFormatType.DATE))); }
cellFormat = new CellFormat(); cellFormat.setAlignment(CellStyle.ALIGN_LEFT); cellFormat.setDataFormat("@"); cellFormat.setWrapText(true);
/** * @see XlsContentProvider#updateRowStyle(ExportRow) */ @Override public MyContentProvider updateRowStyle(final ExportRow row) { for (final ExportCell cell : row.getCells()) { final CellFormat format = cell.ensureAndGetCellFormat(); format.setFillForegroundColor(HSSFColor.WHITE.index); switch (row.getRowNum()) { case 0: format.setFont(FONT_HEADER); break; case 1: format.setFont(FONT_NORMAL_BOLD); format.setFillForegroundColor(HSSFColor.YELLOW.index); break; default: format.setFont(FONT_NORMAL); if (row.getRowNum() % 2 == 0) { format.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); } break; } } return this; }
@Override public XlsContentProvider updateCellStyle(final ExportCell cell) { final CellFormat format = cell.ensureAndGetCellFormat(); CellStyle cellStyle = reusableCellFormats.get(format); if (cellStyle == null) { cellStyle = workbook.createCellStyle(); reusableCellFormats.put(format, cellStyle); format.copyToCellStyle(cellStyle); if (format.getFillForegroundColor() != null) { cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); } cellStyle.setBorderBottom((short) 1); cellStyle.setBorderLeft((short) 1); cellStyle.setBorderRight((short) 1); cellStyle.setBorderTop((short) 1); cellStyle.setWrapText(true); final String dataFormat = format.getDataFormat(); if (dataFormat != null) { final short df = workbook.getDataFormat(format.getDataFormat()); cellStyle.setDataFormat(df); } } cell.setCellStyle(cellStyle); return this; }
/** * @see XlsContentProvider#getCellFormat(ExportCell, java.lang.Object, java.lang.String, * java.util.Map) */ @Override protected CellFormat getCustomizedCellFormat(final CellFormat format, final Object value) { if (value == null || DateHolder.class.isAssignableFrom(value.getClass()) == false) { return null; } if (format != null && BooleanUtils.isTrue(format.getAutoDatePrecision()) == false) { return null; } // Find a format dependent on the precision: final DatePrecision precision = ((DateHolder) value).getPrecision(); if (precision == DatePrecision.DAY) { return new CellFormat(DateFormats.getExcelFormatString(DateFormatType.DATE)); } else if (precision == DatePrecision.SECOND) { return new CellFormat(DateFormats.getExcelFormatString(DateFormatType.DATE_TIME_SECONDS)); } else if (precision == DatePrecision.MILLISECOND) { return new CellFormat(DateFormats.getExcelFormatString(DateFormatType.DATE_TIME_MILLIS)); } else { // HOUR_OF_DAY, MINUTE, MINUTE_15 or null return new CellFormat(DateFormats.getExcelFormatString(DateFormatType.DATE_TIME_MINUTES)); } } }
/** * @param cellFormat * @return this for chaining. */ public ExportCell setCellFormat(final String cellFormat) { this.cellFormat = new CellFormat(cellFormat); return this; }
return null; return format.clone();
@Override public MyContentProvider updateRowStyle(final ExportRow row) { for (final ExportCell cell : row.getCells()) { final CellFormat format = cell.ensureAndGetCellFormat(); format.setFillForegroundColor(HSSFColor.WHITE.index); switch (row.getRowNum()) { case 0: format.setFont(FONT_HEADER); break; case 1: format.setFont(FONT_NORMAL_BOLD); // alignment = CellStyle.ALIGN_CENTER; break; default: format.setFont(FONT_NORMAL); if (row.getRowNum() % 2 == 0) { format.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); } break; } } return this; }
@Override protected CellFormat clone() { final CellFormat clone = new CellFormat(); clone.alignment = this.alignment; clone.autoDatePrecision = this.autoDatePrecision; clone.dataFormat = this.dataFormat; clone.fillForegroundColor = this.fillForegroundColor; clone.font = this.font; clone.wrapText = this.wrapText; return clone; } }
@Override public MyContentProvider updateRowStyle(final ExportRow row) { for (final ExportCell cell : row.getCells()) { final CellFormat format = cell.ensureAndGetCellFormat(); format.setFillForegroundColor(HSSFColor.WHITE.index); switch (row.getRowNum()) { case 0: format.setFont(FONT_NORMAL_BOLD); // alignment = CellStyle.ALIGN_CENTER; break; default: format.setFont(FONT_NORMAL); if (row.getRowNum() % 2 == 0) { format.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); } break; } } return this; }
public CellFormat ensureAndGetCellFormat() { if (cellFormat == null) { cellFormat = new CellFormat(); } return cellFormat; }
@Override public MyContentProvider updateRowStyle(final ExportRow row) { for (final ExportCell cell : row.getCells()) { final CellFormat format = cell.ensureAndGetCellFormat(); format.setFillForegroundColor(HSSFColor.WHITE.index); switch (row.getRowNum()) { case 0: format.setFont(FONT_HEADER); break; case 1: format.setFont(FONT_NORMAL_BOLD); // alignment = CellStyle.ALIGN_CENTER; break; default: format.setFont(FONT_NORMAL); if (row.getRowNum() % 2 == 0) { format.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); } break; } } return this; }
@Override public XlsContentProvider putFormat(final Object obj, final String dataFormat) { formatMap.put(obj, new CellFormat(dataFormat)); return this; }
for (final ExportCell cell : row.getCells()) { final CellFormat format = cell.ensureAndGetCellFormat(); format.setFillForegroundColor(HSSFColor.WHITE.index); switch (row.getRowNum()) { format.setFont(FONT_NORMAL_BOLD); format.setFont(FONT_NORMAL); if (row.getRowNum() % 2 == 0) { format.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
public XlsContentProvider(final ExportContext exportContext, final ExportWorkbook workbook) { this.exportContext = exportContext; this.workbook = workbook; createFonts(); defaultFormatMap.put(Integer.class, new CellFormat("#,##0", CellStyle.ALIGN_RIGHT)); defaultFormatMap.put(Number.class, new CellFormat("#,###.######", CellStyle.ALIGN_RIGHT)); defaultFormatMap .put(Date.class, new CellFormat(ExcelDateFormats.getExcelFormatString(exportContext, DateFormatType.DATE_TIME_MINUTES))); defaultFormatMap.put(java.sql.Date.class, new CellFormat(ExcelDateFormats.getExcelFormatString(exportContext, DateFormatType.DATE))); defaultFormatMap.put(java.sql.Timestamp.class, new CellFormat(ExcelDateFormats.getExcelFormatString(exportContext, DateFormatType.DATE_TIME_MILLIS))); }