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;
33 class ReportExcel
implements TableReportModule {
35 private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
36 private static ReportExcel instance;
37 private static final int EXCEL_CELL_MAXIMUM_SIZE = 36767;
41 private CellStyle titleStyle;
42 private CellStyle setStyle;
43 private CellStyle elementStyle;
44 private int rowIndex = 0;
45 private int sheetColCount = 0;
46 private String reportPath;
49 public static synchronized ReportExcel getDefault() {
50 if (instance == null) {
51 instance =
new ReportExcel();
57 private ReportExcel() {
67 public void startReport(String baseReportDir) {
69 this.reportPath = baseReportDir + getRelativeFilePath();
72 wb =
new XSSFWorkbook();
79 titleStyle = wb.createCellStyle();
81 Font titleFont = wb.createFont();
82 titleFont.setFontHeightInPoints((
short) 12);
83 titleStyle.setFont(titleFont);
84 titleStyle.setAlignment(HorizontalAlignment.LEFT);
85 titleStyle.setWrapText(
true);
87 setStyle = wb.createCellStyle();
88 Font setFont = wb.createFont();
89 setFont.setFontHeightInPoints((
short) 14);
90 setFont.setBold(
true);
91 setStyle.setFont(setFont);
92 setStyle.setAlignment(HorizontalAlignment.LEFT);
93 setStyle.setWrapText(
true);
95 elementStyle = wb.createCellStyle();
97 Font elementFont = wb.createFont();
98 elementFont.setFontHeightInPoints((
short) 14);
99 elementStyle.setFont(elementFont);
100 elementStyle.setAlignment(HorizontalAlignment.LEFT);
101 elementStyle.setWrapText(
true);
103 writeSummaryWorksheet();
110 public void endReport() {
111 FileOutputStream out = null;
113 out =
new FileOutputStream(reportPath);
115 Case.getCurrentCase().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
116 "ReportExcel.endReport.srcModuleName.text"),
"");
117 }
catch (IOException ex) {
118 logger.log(Level.SEVERE,
"Failed to write Excel report.", ex);
119 }
catch (TskCoreException ex) {
120 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
121 logger.log(Level.SEVERE, errorMessage, ex);
126 }
catch (IOException ex) {
140 public void startDataType(String name, String description) {
142 name = escapeForExcel(name);
143 sheet = wb.createSheet(name);
144 sheet.setAutobreaks(
true);
155 public void endDataType() {
157 for (
int i = 0; i < sheetColCount; ++i) {
158 sheet.autoSizeColumn(i);
168 public void startSet(String setName) {
169 setName = escapeForExcel(setName);
170 Row row = sheet.createRow(rowIndex);
171 row.setRowStyle(setStyle);
172 row.createCell(0).setCellValue(setName);
180 public void endSet() {
182 sheet.createRow(rowIndex);
187 public void addSetIndex(List<String> sets) {
197 public void addSetElement(String elementName) {
198 elementName = escapeForExcel(elementName);
199 Row row = sheet.createRow(rowIndex);
200 row.setRowStyle(elementStyle);
201 row.createCell(0).setCellValue(elementName);
211 public void startTable(List<String> titles) {
212 int tableColCount = 0;
213 Row row = sheet.createRow(rowIndex);
214 row.setRowStyle(titleStyle);
215 for (
int i = 0; i < titles.size(); i++) {
216 row.createCell(i).setCellValue(titles.get(i));
222 if (tableColCount > sheetColCount) {
223 sheetColCount = tableColCount;
228 public void endTable() {
230 sheet.createRow(rowIndex);
241 "ReportExcel.exceptionMessage.dataTooLarge=Value is too long to fit into an Excel cell. ",
242 "ReportExcel.exceptionMessage.errorText=Error showing data into an Excel cell."
245 public void addRow(List<String> rowData) {
246 Row row = sheet.createRow(rowIndex);
247 for (
int i = 0; i < rowData.size(); ++i) {
248 Cell excelCell = row.createCell(i);
250 excelCell.setCellValue(rowData.get(i));
251 }
catch (Exception e) {
252 if (e instanceof java.lang.IllegalArgumentException && rowData.get(i).length() > EXCEL_CELL_MAXIMUM_SIZE) {
253 excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_dataTooLarge() + e.getMessage());
255 excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_errorText());
270 public String dateToString(
long date) {
271 SimpleDateFormat sdf =
new java.text.SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
272 return sdf.format(
new java.util.Date(date * 1000));
276 public String getName() {
277 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getName.text");
281 public String getDescription() {
282 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getDesc.text");
286 public String getRelativeFilePath() {
298 private static String escapeForExcel(String text) {
299 return text.replaceAll(
"[\\/\\:\\?\\*\\\\]",
"_");
302 private void writeSummaryWorksheet() {
303 sheet = wb.createSheet(NbBundle.getMessage(
this.getClass(),
"ReportExcel.sheetName.text"));
306 Row row = sheet.createRow(rowIndex);
307 row.setRowStyle(setStyle);
308 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.summary"));
311 sheet.createRow(rowIndex);
314 Case currentCase = Case.getCurrentCase();
316 row = sheet.createRow(rowIndex);
317 row.setRowStyle(setStyle);
318 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseName"));
319 row.createCell(1).setCellValue(currentCase.getDisplayName());
322 row = sheet.createRow(rowIndex);
323 row.setRowStyle(setStyle);
324 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseNum"));
325 row.createCell(1).setCellValue(currentCase.getNumber());
328 row = sheet.createRow(rowIndex);
329 row.setRowStyle(setStyle);
330 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.examiner"));
331 row.createCell(1).setCellValue(currentCase.getExaminer());
334 row = sheet.createRow(rowIndex);
335 row.setRowStyle(setStyle);
336 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.numImages"));
339 numImages = currentCase.getDataSources().size();
340 }
catch (TskCoreException ex) {
343 row.createCell(1).setCellValue(numImages);
346 sheet.autoSizeColumn(0);
347 sheet.autoSizeColumn(1);