19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Objects;
30 import java.util.Optional;
31 import org.apache.poi.ss.usermodel.Cell;
32 import org.apache.poi.ss.usermodel.CellStyle;
33 import org.apache.poi.ss.usermodel.Font;
34 import org.apache.poi.ss.usermodel.HorizontalAlignment;
35 import org.apache.poi.ss.usermodel.Row;
36 import org.apache.poi.ss.usermodel.Workbook;
37 import org.apache.poi.ss.usermodel.Sheet;
38 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
39 import org.openide.util.NbBundle.Messages;
68 super(
string, thrwbl);
76 static class CellStyleKey {
78 private final String formatString;
79 private final CellStyle cellStyle;
92 CellStyleKey(String formatString, CellStyle cellStyle,
HorizontalAlign alignment) {
93 this.formatString = formatString;
94 this.cellStyle = cellStyle;
95 this.alignment = alignment;
101 String getFormatString() {
108 CellStyle getCellStyle() {
115 HorizontalAlign getAlignment() {
120 public int hashCode() {
122 hash = 29 * hash + Objects.hashCode(this.formatString);
123 hash = 29 * hash + Objects.hashCode(this.cellStyle);
124 hash = 29 * hash + Objects.hashCode(this.alignment);
129 public boolean equals(Object obj) {
136 if (getClass() != obj.getClass()) {
139 final CellStyleKey other = (CellStyleKey) obj;
140 if (!Objects.equals(
this.formatString, other.formatString)) {
143 if (!Objects.equals(
this.cellStyle, other.cellStyle)) {
146 if (this.alignment != other.alignment) {
172 WorksheetEnv(CellStyle headerStyle, CellStyle defaultStyle, Workbook parentWorkbook) {
186 return cellStyleCache.computeIfAbsent(cellStyleKey, (pair) -> {
187 CellStyle computed = this.parentWorkbook.createCellStyle();
188 computed.cloneStyleFrom(cellStyleKey.getCellStyle() == null ? defaultStyle : cellStyleKey.getCellStyle());
190 if (cellStyleKey.getAlignment() != null) {
191 computed.setAlignment(cellStyleKey.getAlignment().getPoiAlignment());
194 if (cellStyleKey.getFormatString() != null) {
195 computed.setDataFormat(this.parentWorkbook.getCreationHelper().createDataFormat().getFormat(cellStyleKey.getFormatString()));
261 if (instance == null) {
281 "# {0} - sheetNumber",
282 "ExcelExport_writeExcel_noSheetName=Sheet {0}"
286 Workbook workbook =
new XSSFWorkbook();
289 Font headerFont = workbook.createFont();
290 headerFont.setBold(
true);
294 HorizontalAlignment alignment = HorizontalAlignment.
LEFT;
295 CellStyle headerCellStyle = workbook.createCellStyle();
296 headerCellStyle.setFont(headerFont);
297 headerCellStyle.setAlignment(alignment);
299 CellStyle defaultCellStyle = workbook.createCellStyle();
300 defaultCellStyle.setAlignment(alignment);
304 if (exports != null) {
305 for (
int i = 0; i < exports.size(); i++) {
307 if (export == null) {
312 if (sheetName == null) {
313 sheetName = Bundle.ExcelExport_writeExcel_noSheetName(i + 1);
316 Sheet sheet = workbook.createSheet(sheetName);
322 FileOutputStream fileOut =
new FileOutputStream(path);
323 workbook.write(fileOut);
340 static Cell createCell(WorksheetEnv env, Row row,
int colNum,
ExcelCellModel cellModel, Optional<CellStyle> cellStyle) {
341 CellStyle cellStyleToUse = cellStyle.orElse(env.getDefaultCellStyle());
347 Object cellData = cellModel.
getData();
348 Cell cell = row.createCell(colNum);
349 if (cellData instanceof Calendar) {
350 cell.setCellValue((Calendar) cellData);
351 }
else if (cellData instanceof Date) {
352 cell.setCellValue((Date) cellData);
353 }
else if (cellData instanceof Double) {
354 cell.setCellValue((Double) cellData);
355 }
else if (cellData instanceof String) {
356 cell.setCellValue((String) cellData);
357 }
else if (cellData instanceof Short) {
358 cell.setCellValue((Short) cellData);
359 }
else if (cellData instanceof Integer) {
360 cell.setCellValue((Integer) cellData);
361 }
else if (cellData instanceof Long) {
362 cell.setCellValue((Long) cellData);
363 }
else if (cellData instanceof Float) {
364 cell.setCellValue((Float) cellData);
366 cell.setCellValue(cellModel.
getText());
368 cell.setCellStyle(cellStyleToUse);
final Map< CellStyleKey, CellStyle > cellStyleCache
Workbook getParentWorkbook()
String getExcelFormatString()
ExcelExportException(String string)
CellStyle getCellStyle(CellStyleKey cellStyleKey)
void writeExcel(List< ExcelSheetExport > exports, File path)
CellStyle getDefaultCellStyle()
final CellStyle headerStyle
static ExcelExport getInstance()
ExcelExportException(String string, Throwable thrwbl)
CellStyle getHeaderStyle()
void renderSheet(Sheet sheet, WorksheetEnv env)
final Workbook parentWorkbook
final CellStyle defaultStyle
HorizontalAlign getHorizontalAlignment()
static ExcelExport instance