Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExcelExport.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
20 
21 import java.io.File;
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;
28 import java.util.Map;
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;
41 
45 class ExcelExport {
46 
50  static class ExcelExportException extends Exception {
51 
57  ExcelExportException(String string) {
58  super(string);
59  }
60 
67  ExcelExportException(String string, Throwable thrwbl) {
68  super(string, thrwbl);
69  }
70  }
71 
76  static class CellStyleKey {
77 
78  private final String formatString;
79  private final CellStyle cellStyle;
80  private final HorizontalAlign alignment;
81 
92  CellStyleKey(String formatString, CellStyle cellStyle, HorizontalAlign alignment) {
93  this.formatString = formatString;
94  this.cellStyle = cellStyle;
95  this.alignment = alignment;
96  }
97 
101  String getFormatString() {
102  return formatString;
103  }
104 
108  CellStyle getCellStyle() {
109  return cellStyle;
110  }
111 
115  HorizontalAlign getAlignment() {
116  return alignment;
117  }
118 
119  @Override
120  public int hashCode() {
121  int hash = 7;
122  hash = 29 * hash + Objects.hashCode(this.formatString);
123  hash = 29 * hash + Objects.hashCode(this.cellStyle);
124  hash = 29 * hash + Objects.hashCode(this.alignment);
125  return hash;
126  }
127 
128  @Override
129  public boolean equals(Object obj) {
130  if (this == obj) {
131  return true;
132  }
133  if (obj == null) {
134  return false;
135  }
136  if (getClass() != obj.getClass()) {
137  return false;
138  }
139  final CellStyleKey other = (CellStyleKey) obj;
140  if (!Objects.equals(this.formatString, other.formatString)) {
141  return false;
142  }
143  if (!Objects.equals(this.cellStyle, other.cellStyle)) {
144  return false;
145  }
146  if (this.alignment != other.alignment) {
147  return false;
148  }
149  return true;
150  }
151  }
152 
156  static class WorksheetEnv {
157 
158  private final CellStyle headerStyle;
159  private final Workbook parentWorkbook;
160  private final CellStyle defaultStyle;
161 
162  // maps a data format string / original cell style combination to a created cell style
163  private final Map<CellStyleKey, CellStyle> cellStyleCache = new HashMap<>();
164 
172  WorksheetEnv(CellStyle headerStyle, CellStyle defaultStyle, Workbook parentWorkbook) {
173  this.headerStyle = headerStyle;
174  this.defaultStyle = defaultStyle;
175  this.parentWorkbook = parentWorkbook;
176  }
177 
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());
189 
190  if (cellStyleKey.getAlignment() != null) {
191  computed.setAlignment(cellStyleKey.getAlignment().getPoiAlignment());
192  }
193 
194  if (cellStyleKey.getFormatString() != null) {
195  computed.setDataFormat(this.parentWorkbook.getCreationHelper().createDataFormat().getFormat(cellStyleKey.getFormatString()));
196  }
197  return computed;
198  });
199  }
200 
206  CellStyle getHeaderStyle() {
207  return headerStyle;
208  }
209 
215  CellStyle getDefaultCellStyle() {
216  return defaultStyle;
217  }
218 
224  Workbook getParentWorkbook() {
225  return parentWorkbook;
226  }
227  }
228 
232  static interface ExcelSheetExport {
233 
241  String getSheetName();
242 
250  void renderSheet(Sheet sheet, WorksheetEnv env) throws ExcelExportException;
251  }
252 
253  private ExcelExport() {
254  }
255 
264  @Messages({
265  "# {0} - sheetNumber",
266  "ExcelExport_writeExcel_noSheetName=Sheet {0}"
267  })
268  static void writeExcel(List<ExcelSheetExport> exports, File path) throws IOException, ExcelExportException {
269  // Create a Workbook
270  Workbook workbook = new XSSFWorkbook(); // new HSSFWorkbook() for generating `.xls` file
271 
272  // Create a Font for styling header cells
273  Font headerFont = workbook.createFont();
274  headerFont.setBold(true);
275  //headerFont.setFontHeightInPoints((short) 14);
276 
277  // Create a CellStyle with the font
278  HorizontalAlignment alignment = HorizontalAlignment.LEFT;
279  CellStyle headerCellStyle = workbook.createCellStyle();
280  headerCellStyle.setFont(headerFont);
281  headerCellStyle.setAlignment(alignment);
282 
283  CellStyle defaultCellStyle = workbook.createCellStyle();
284  defaultCellStyle.setAlignment(alignment);
285 
286  WorksheetEnv env = new WorksheetEnv(headerCellStyle, defaultCellStyle, workbook);
287 
288  if (exports != null) {
289  for (int i = 0; i < exports.size(); i++) {
290  ExcelSheetExport export = exports.get(i);
291  if (export == null) {
292  continue;
293  }
294 
295  String sheetName = export.getSheetName();
296  if (sheetName == null) {
297  sheetName = Bundle.ExcelExport_writeExcel_noSheetName(i + 1);
298  }
299 
300  Sheet sheet = workbook.createSheet(sheetName);
301  export.renderSheet(sheet, env);
302  }
303  }
304 
305  // Write the output to a file
306  FileOutputStream fileOut = new FileOutputStream(path);
307  workbook.write(fileOut);
308  fileOut.close();
309 
310  // Closing the workbook
311  workbook.close();
312  }
313 
324  static Cell createCell(WorksheetEnv env, Row row, int colNum, CellModel cellModel, Optional<CellStyle> cellStyle) {
325  CellStyle cellStyleToUse = cellStyle.orElse(env.getDefaultCellStyle());
326 
327  if (cellModel.getExcelFormatString() != null || cellModel.getHorizontalAlignment() != null) {
328  cellStyleToUse = env.getCellStyle(new CellStyleKey(cellModel.getExcelFormatString(), cellStyleToUse, cellModel.getHorizontalAlignment()));
329  }
330 
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);
349  } else {
350  cell.setCellValue(cellModel.getText());
351  }
352  cell.setCellStyle(cellStyleToUse);
353  return cell;
354  }
355 }

Copyright © 2012-2022 Basis Technology. Generated on: Sun Sep 25 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.