19 package org.sleuthkit.autopsy.report;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.text.SimpleDateFormat;
24 import java.util.List;
25 import java.util.logging.Level;
26 import org.apache.poi.ss.usermodel.*;
27 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
28 import org.openide.util.NbBundle;
34 class ReportExcel
implements TableReportModule {
36 private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
37 private static ReportExcel instance;
38 private static final int EXCEL_CELL_MAXIMUM_SIZE = 36767;
42 private CellStyle titleStyle;
43 private CellStyle setStyle;
44 private CellStyle elementStyle;
45 private int rowIndex = 0;
46 private int sheetColCount = 0;
47 private String reportPath;
50 public static synchronized ReportExcel getDefault() {
51 if (instance == null) {
52 instance =
new ReportExcel();
58 private ReportExcel() {
68 public void startReport(String baseReportDir) {
70 this.reportPath = baseReportDir + getRelativeFilePath();
73 wb =
new XSSFWorkbook();
80 titleStyle = wb.createCellStyle();
82 Font titleFont = wb.createFont();
83 titleFont.setFontHeightInPoints((
short) 12);
84 titleStyle.setFont(titleFont);
85 titleStyle.setAlignment(HorizontalAlignment.LEFT);
86 titleStyle.setWrapText(
true);
88 setStyle = wb.createCellStyle();
89 Font setFont = wb.createFont();
90 setFont.setFontHeightInPoints((
short) 14);
91 setFont.setBold(
true);
92 setStyle.setFont(setFont);
93 setStyle.setAlignment(HorizontalAlignment.LEFT);
94 setStyle.setWrapText(
true);
96 elementStyle = wb.createCellStyle();
98 Font elementFont = wb.createFont();
99 elementFont.setFontHeightInPoints((
short) 14);
100 elementStyle.setFont(elementFont);
101 elementStyle.setAlignment(HorizontalAlignment.LEFT);
102 elementStyle.setWrapText(
true);
104 writeSummaryWorksheet();
111 public void endReport() {
112 FileOutputStream out = null;
114 out =
new FileOutputStream(reportPath);
116 Case.getOpenCase().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
117 "ReportExcel.endReport.srcModuleName.text"),
"");
118 }
catch (IOException ex) {
119 logger.log(Level.SEVERE,
"Failed to write Excel report.", ex);
120 }
catch (TskCoreException ex) {
121 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
122 logger.log(Level.SEVERE, errorMessage, ex);
123 }
catch (NoCurrentCaseException ex) {
124 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
129 }
catch (IOException ex) {
143 public void startDataType(String name, String description) {
145 name = escapeForExcel(name);
146 sheet = wb.createSheet(name);
147 sheet.setAutobreaks(
true);
158 public void endDataType() {
160 for (
int i = 0; i < sheetColCount; ++i) {
161 sheet.autoSizeColumn(i);
171 public void startSet(String setName) {
172 setName = escapeForExcel(setName);
173 Row row = sheet.createRow(rowIndex);
174 row.setRowStyle(setStyle);
175 row.createCell(0).setCellValue(setName);
183 public void endSet() {
185 sheet.createRow(rowIndex);
190 public void addSetIndex(List<String> sets) {
200 public void addSetElement(String elementName) {
201 elementName = escapeForExcel(elementName);
202 Row row = sheet.createRow(rowIndex);
203 row.setRowStyle(elementStyle);
204 row.createCell(0).setCellValue(elementName);
214 public void startTable(List<String> titles) {
215 int tableColCount = 0;
216 Row row = sheet.createRow(rowIndex);
217 row.setRowStyle(titleStyle);
218 for (
int i = 0; i < titles.size(); i++) {
219 row.createCell(i).setCellValue(titles.get(i));
225 if (tableColCount > sheetColCount) {
226 sheetColCount = tableColCount;
231 public void endTable() {
233 sheet.createRow(rowIndex);
244 "ReportExcel.exceptionMessage.dataTooLarge=Value is too long to fit into an Excel cell. ",
245 "ReportExcel.exceptionMessage.errorText=Error showing data into an Excel cell."
248 public void addRow(List<String> rowData) {
249 Row row = sheet.createRow(rowIndex);
250 for (
int i = 0; i < rowData.size(); ++i) {
251 Cell excelCell = row.createCell(i);
253 excelCell.setCellValue(rowData.get(i));
254 }
catch (Exception e) {
255 if (e instanceof java.lang.IllegalArgumentException && rowData.get(i).length() > EXCEL_CELL_MAXIMUM_SIZE) {
256 excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_dataTooLarge() + e.getMessage());
258 excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_errorText());
273 public String dateToString(
long date) {
274 SimpleDateFormat sdf =
new java.text.SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
275 return sdf.format(
new java.util.Date(date * 1000));
279 public String getName() {
280 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getName.text");
284 public String getDescription() {
285 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getDesc.text");
289 public String getRelativeFilePath() {
301 private static String escapeForExcel(String text) {
302 return text.replaceAll(
"[\\/\\:\\?\\*\\\\]",
"_");
305 private void writeSummaryWorksheet() {
308 currentCase = Case.getOpenCase();
309 }
catch (NoCurrentCaseException ex) {
310 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
313 sheet = wb.createSheet(NbBundle.getMessage(
this.getClass(),
"ReportExcel.sheetName.text"));
316 Row row = sheet.createRow(rowIndex);
317 row.setRowStyle(setStyle);
318 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.summary"));
321 sheet.createRow(rowIndex);
324 row = sheet.createRow(rowIndex);
325 row.setRowStyle(setStyle);
326 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseName"));
327 row.createCell(1).setCellValue(currentCase.getDisplayName());
330 row = sheet.createRow(rowIndex);
331 row.setRowStyle(setStyle);
332 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseNum"));
333 row.createCell(1).setCellValue(currentCase.getNumber());
336 row = sheet.createRow(rowIndex);
337 row.setRowStyle(setStyle);
338 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.examiner"));
339 row.createCell(1).setCellValue(currentCase.getExaminer());
342 row = sheet.createRow(rowIndex);
343 row.setRowStyle(setStyle);
344 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.numImages"));
347 numImages = currentCase.getDataSources().size();
348 }
catch (TskCoreException ex) {
351 row.createCell(1).setCellValue(numImages);
354 sheet.autoSizeColumn(0);
355 sheet.autoSizeColumn(1);