Autopsy  4.19.0
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.datasourcesummary.uiutils;
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 public class ExcelExport {
46 
50  public static class ExcelExportException extends Exception {
51 
57  public ExcelExportException(String string) {
58  super(string);
59  }
60 
67  public 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  public 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  public 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  public CellStyle getHeaderStyle() {
207  return headerStyle;
208  }
209 
215  public CellStyle getDefaultCellStyle() {
216  return defaultStyle;
217  }
218 
224  public Workbook getParentWorkbook() {
225  return parentWorkbook;
226  }
227  }
228 
232  public static interface ExcelSheetExport {
233 
241  String getSheetName();
242 
250  void renderSheet(Sheet sheet, WorksheetEnv env) throws ExcelExportException;
251  }
252 
253  private static ExcelExport instance = null;
254 
260  public static ExcelExport getInstance() {
261  if (instance == null) {
262  instance = new ExcelExport();
263  }
264 
265  return instance;
266  }
267 
268  private ExcelExport() {
269 
270  }
271 
280  @Messages({
281  "# {0} - sheetNumber",
282  "ExcelExport_writeExcel_noSheetName=Sheet {0}"
283  })
284  public void writeExcel(List<ExcelSheetExport> exports, File path) throws IOException, ExcelExportException {
285  // Create a Workbook
286  Workbook workbook = new XSSFWorkbook(); // new HSSFWorkbook() for generating `.xls` file
287 
288  // Create a Font for styling header cells
289  Font headerFont = workbook.createFont();
290  headerFont.setBold(true);
291  //headerFont.setFontHeightInPoints((short) 14);
292 
293  // Create a CellStyle with the font
294  HorizontalAlignment alignment = HorizontalAlignment.LEFT;
295  CellStyle headerCellStyle = workbook.createCellStyle();
296  headerCellStyle.setFont(headerFont);
297  headerCellStyle.setAlignment(alignment);
298 
299  CellStyle defaultCellStyle = workbook.createCellStyle();
300  defaultCellStyle.setAlignment(alignment);
301 
302  WorksheetEnv env = new WorksheetEnv(headerCellStyle, defaultCellStyle, workbook);
303 
304  if (exports != null) {
305  for (int i = 0; i < exports.size(); i++) {
306  ExcelSheetExport export = exports.get(i);
307  if (export == null) {
308  continue;
309  }
310 
311  String sheetName = export.getSheetName();
312  if (sheetName == null) {
313  sheetName = Bundle.ExcelExport_writeExcel_noSheetName(i + 1);
314  }
315 
316  Sheet sheet = workbook.createSheet(sheetName);
317  export.renderSheet(sheet, env);
318  }
319  }
320 
321  // Write the output to a file
322  FileOutputStream fileOut = new FileOutputStream(path);
323  workbook.write(fileOut);
324  fileOut.close();
325 
326  // Closing the workbook
327  workbook.close();
328  }
329 
340  static Cell createCell(WorksheetEnv env, Row row, int colNum, ExcelCellModel cellModel, Optional<CellStyle> cellStyle) {
341  CellStyle cellStyleToUse = cellStyle.orElse(env.getDefaultCellStyle());
342 
343  if (cellModel.getExcelFormatString() != null || cellModel.getHorizontalAlignment() != null) {
344  cellStyleToUse = env.getCellStyle(new CellStyleKey(cellModel.getExcelFormatString(), cellStyleToUse, cellModel.getHorizontalAlignment()));
345  }
346 
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);
365  } else {
366  cell.setCellValue(cellModel.getText());
367  }
368  cell.setCellStyle(cellStyleToUse);
369  return cell;
370  }
371 }
void writeExcel(List< ExcelSheetExport > exports, File path)

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.