19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Optional;
24 import org.apache.poi.ss.usermodel.Cell;
25 import org.apache.poi.ss.usermodel.Row;
26 import org.apache.poi.ss.usermodel.Sheet;
35 class ExcelTableExport<T, C
extends CellModel> implements ExcelSheetExport, ExcelItemExportable {
37 private final String sheetName;
38 private final List<ColumnModel<T, C>> columns;
39 private final List<T> data;
40 private final int columnIndent;
50 ExcelTableExport(String sheetName, List<ColumnModel<T, C>> columns, List<T> data) {
51 this(sheetName, columns, data, 0);
63 ExcelTableExport(String sheetName, List<ColumnModel<T, C>> columns, List<T> data,
int columnIndent) {
64 this.sheetName = sheetName;
65 this.columns = columns;
67 this.columnIndent = columnIndent;
71 public String getSheetName() {
76 public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv style) throws ExcelExport.ExcelExportException {
77 renderSheet(sheet, style, 0, columnIndent, columns, data);
80 for (
int i = 0; i < columns.size(); i++) {
81 sheet.autoSizeColumn(i);
85 sheet.createFreezePane(0, 1);
89 public ItemDimensions write(Sheet sheet,
int rowStart,
int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
90 int columnStart = columnIndent + colStart;
91 int rowsWritten = renderSheet(sheet, env, rowStart, columnStart, columns, data);
92 return new ItemDimensions(rowStart, columnStart, rowStart + rowsWritten - 1, this.columns == null ? columnStart : columnStart + this.columns.size());
107 private static <T, C extends CellModel>
int renderSheet(
109 ExcelExport.WorksheetEnv worksheetEnv,
112 List<ColumnModel<T, C>> columns, List<T> data)
113 throws ExcelExport.ExcelExportException {
115 List<T> safeData = data == null ? Collections.emptyList() : data;
117 Row headerRow = sheet.createRow(rowStart);
119 for (
int i = 0; i < columns.size(); i++) {
120 Cell cell = headerRow.createCell(i + colStart);
121 cell.setCellValue(columns.get(i).getHeaderTitle());
122 cell.setCellStyle(worksheetEnv.getHeaderStyle());
126 for (
int rowNum = 0; rowNum < safeData.size(); rowNum++) {
127 T rowData = safeData.get(rowNum);
128 Row row = sheet.createRow(rowNum + rowStart + 1);
129 for (
int colNum = 0; colNum < columns.size(); colNum++) {
130 ColumnModel<T, ? extends CellModel> colModel = columns.get(colNum);
131 CellModel cellModel = colModel.getCellRenderer().apply(rowData);
132 ExcelExport.createCell(worksheetEnv, row, colNum + colStart, cellModel, Optional.empty());
136 return safeData.size() + 1;