19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
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;
50 static class ExcelExportException
extends Exception {
57 ExcelExportException(String
string) {
67 ExcelExportException(String
string, Throwable thrwbl) {
68 super(
string, thrwbl);
76 static class CellStyleKey {
78 private final String formatString;
79 private final CellStyle cellStyle;
80 private final HorizontalAlign alignment;
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) {
156 static class WorksheetEnv {
158 private final CellStyle headerStyle;
159 private final Workbook parentWorkbook;
160 private final CellStyle defaultStyle;
163 private final Map<CellStyleKey, CellStyle> cellStyleCache =
new HashMap<>();
172 WorksheetEnv(CellStyle headerStyle, CellStyle defaultStyle, Workbook parentWorkbook) {
173 this.headerStyle = headerStyle;
174 this.defaultStyle = defaultStyle;
175 this.parentWorkbook = parentWorkbook;
185 CellStyle getCellStyle(CellStyleKey cellStyleKey) {
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()));
206 CellStyle getHeaderStyle() {
215 CellStyle getDefaultCellStyle() {
224 Workbook getParentWorkbook() {
225 return parentWorkbook;
232 static interface ExcelSheetExport {
241 String getSheetName();
250 void renderSheet(Sheet sheet, WorksheetEnv env)
throws ExcelExportException;
253 private ExcelExport() {
265 "# {0} - sheetNumber",
266 "ExcelExport_writeExcel_noSheetName=Sheet {0}"
268 static void writeExcel(List<ExcelSheetExport> exports, File path)
throws IOException, ExcelExportException {
270 Workbook workbook =
new XSSFWorkbook();
273 Font headerFont = workbook.createFont();
274 headerFont.setBold(
true);
278 HorizontalAlignment alignment = HorizontalAlignment.LEFT;
279 CellStyle headerCellStyle = workbook.createCellStyle();
280 headerCellStyle.setFont(headerFont);
281 headerCellStyle.setAlignment(alignment);
283 CellStyle defaultCellStyle = workbook.createCellStyle();
284 defaultCellStyle.setAlignment(alignment);
286 WorksheetEnv env =
new WorksheetEnv(headerCellStyle, defaultCellStyle, workbook);
288 if (exports != null) {
289 for (
int i = 0; i < exports.size(); i++) {
290 ExcelSheetExport export = exports.get(i);
291 if (export == null) {
295 String sheetName = export.getSheetName();
296 if (sheetName == null) {
297 sheetName = Bundle.ExcelExport_writeExcel_noSheetName(i + 1);
300 Sheet sheet = workbook.createSheet(sheetName);
301 export.renderSheet(sheet, env);
306 FileOutputStream fileOut =
new FileOutputStream(path);
307 workbook.write(fileOut);
324 static Cell createCell(WorksheetEnv env, Row row,
int colNum, CellModel cellModel, Optional<CellStyle> cellStyle) {
325 CellStyle cellStyleToUse = cellStyle.orElse(env.getDefaultCellStyle());
327 if (cellModel.getExcelFormatString() != null || cellModel.getHorizontalAlignment() != null) {
328 cellStyleToUse = env.getCellStyle(
new CellStyleKey(cellModel.getExcelFormatString(), cellStyleToUse, cellModel.getHorizontalAlignment()));
331 Object cellData = cellModel.getData();
332 Cell cell = row.createCell(colNum);
333 if (cellData instanceof Calendar) {
334 cell.setCellValue((Calendar) cellData);
335 }
else if (cellData instanceof Date) {
336 cell.setCellValue((Date) cellData);
337 }
else if (cellData instanceof Double) {
338 cell.setCellValue((Double) cellData);
339 }
else if (cellData instanceof String) {
340 cell.setCellValue((String) cellData);
341 }
else if (cellData instanceof Short) {
342 cell.setCellValue((Short) cellData);
343 }
else if (cellData instanceof Integer) {
344 cell.setCellValue((Integer) cellData);
345 }
else if (cellData instanceof Long) {
346 cell.setCellValue((Long) cellData);
347 }
else if (cellData instanceof Float) {
348 cell.setCellValue((Float) cellData);
350 cell.setCellValue(cellModel.getText());
352 cell.setCellStyle(cellStyleToUse);