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 {
34 private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
35 private static ReportExcel instance;
39 private CellStyle titleStyle;
40 private CellStyle setStyle;
41 private CellStyle elementStyle;
42 private int rowIndex = 0;
43 private int sheetColCount = 0;
44 private String reportPath;
47 public static synchronized ReportExcel getDefault() {
48 if (instance == null) {
49 instance =
new ReportExcel();
55 private ReportExcel() {
64 public void startReport(String baseReportDir) {
66 this.reportPath = baseReportDir + getRelativeFilePath();
69 wb =
new XSSFWorkbook();
76 titleStyle = wb.createCellStyle();
78 Font titleFont = wb.createFont();
79 titleFont.setFontHeightInPoints((
short) 12);
80 titleStyle.setFont(titleFont);
81 titleStyle.setAlignment(CellStyle.ALIGN_LEFT);
82 titleStyle.setWrapText(
true);
84 setStyle = wb.createCellStyle();
85 Font setFont = wb.createFont();
86 setFont.setFontHeightInPoints((
short) 14);
87 setFont.setBoldweight((
short) 10);
88 setStyle.setFont(setFont);
89 setStyle.setAlignment(CellStyle.ALIGN_LEFT);
90 setStyle.setWrapText(
true);
92 elementStyle = wb.createCellStyle();
94 Font elementFont = wb.createFont();
95 elementFont.setFontHeightInPoints((
short) 14);
96 elementStyle.setFont(elementFont);
97 elementStyle.setAlignment(CellStyle.ALIGN_LEFT);
98 elementStyle.setWrapText(
true);
100 writeSummaryWorksheet();
107 public void endReport() {
108 FileOutputStream out = null;
110 out =
new FileOutputStream(reportPath);
112 Case.getCurrentCase().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
113 "ReportExcel.endReport.srcModuleName.text"),
"");
114 }
catch (IOException ex) {
115 logger.log(Level.SEVERE,
"Failed to write Excel report.", ex);
116 }
catch (TskCoreException ex) {
117 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
118 logger.log(Level.SEVERE, errorMessage, ex);
123 }
catch (IOException ex) {
137 public void startDataType(String name, String description) {
139 name = escapeForExcel(name);
140 sheet = wb.createSheet(name);
141 sheet.setAutobreaks(
true);
152 public void endDataType() {
154 for (
int i = 0; i < sheetColCount; ++i) {
155 sheet.autoSizeColumn(i);
164 public void startSet(String setName) {
165 setName = escapeForExcel(setName);
166 Row row = sheet.createRow(rowIndex);
167 row.setRowStyle(setStyle);
168 row.createCell(0).setCellValue(setName);
176 public void endSet() {
178 sheet.createRow(rowIndex);
183 public void addSetIndex(List<String> sets) {
192 public void addSetElement(String elementName) {
193 elementName = escapeForExcel(elementName);
194 Row row = sheet.createRow(rowIndex);
195 row.setRowStyle(elementStyle);
196 row.createCell(0).setCellValue(elementName);
205 public void startTable(List<String> titles) {
206 int tableColCount = 0;
207 Row row = sheet.createRow(rowIndex);
208 row.setRowStyle(titleStyle);
209 for (
int i=0; i<titles.size(); i++) {
210 row.createCell(i).setCellValue(titles.get(i));
216 if (tableColCount > sheetColCount) {
217 sheetColCount = tableColCount;
222 public void endTable() {
224 sheet.createRow(rowIndex);
233 public void addRow(List<String> rowData) {
234 Row row = sheet.createRow(rowIndex);
235 for (
int i = 0; i < rowData.size(); ++i) {
236 row.createCell(i).setCellValue(rowData.get(i));
247 public String dateToString(
long date) {
248 SimpleDateFormat sdf =
new java.text.SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
249 return sdf.format(
new java.util.Date(date * 1000));
253 public String getName() {
254 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getName.text");
258 public String getDescription() {
259 return NbBundle.getMessage(this.getClass(),
"ReportExcel.getDesc.text");
263 public String getRelativeFilePath() {
273 private static String escapeForExcel(String text) {
274 return text.replaceAll(
"[\\/\\:\\?\\*\\\\]",
"_");
277 private void writeSummaryWorksheet() {
278 sheet = wb.createSheet(NbBundle.getMessage(
this.getClass(),
"ReportExcel.sheetName.text"));
281 Row row = sheet.createRow(rowIndex);
282 row.setRowStyle(setStyle);
283 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.summary"));
286 sheet.createRow(rowIndex);
289 Case currentCase = Case.getCurrentCase();
291 row = sheet.createRow(rowIndex);
292 row.setRowStyle(setStyle);
293 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseName"));
294 row.createCell(1).setCellValue(currentCase.getName());
297 row = sheet.createRow(rowIndex);
298 row.setRowStyle(setStyle);
299 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.caseNum"));
300 row.createCell(1).setCellValue(currentCase.getNumber());
303 row = sheet.createRow(rowIndex);
304 row.setRowStyle(setStyle);
305 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.examiner"));
306 row.createCell(1).setCellValue(currentCase.getExaminer());
309 row = sheet.createRow(rowIndex);
310 row.setRowStyle(setStyle);
311 row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(),
"ReportExcel.cellVal.numImages"));
314 numImages = currentCase.getDataSources().size();
315 }
catch (TskCoreException ex) {
318 row.createCell(1).setCellValue(numImages);
321 sheet.autoSizeColumn(0);
322 sheet.autoSizeColumn(1);