Autopsy  4.18.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExcelTableExport.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.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;
31 
35 public class ExcelTableExport<T, C extends ExcelCellModel> implements ExcelSheetExport, ExcelItemExportable {
36 
37  private final String sheetName;
38  private final List<ColumnModel<T, C>> columns;
39  private final List<T> data;
40  private final int columnIndent;
41 
50  public ExcelTableExport(String sheetName, List<ColumnModel<T, C>> columns, List<T> data) {
51  this(sheetName, columns, data, 0);
52  }
53 
63  public ExcelTableExport(String sheetName, List<ColumnModel<T, C>> columns, List<T> data, int columnIndent) {
64  this.sheetName = sheetName;
65  this.columns = columns;
66  this.data = data;
67  this.columnIndent = columnIndent;
68  }
69 
70  @Override
71  public String getSheetName() {
72  return sheetName;
73  }
74 
75  @Override
76  public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv style) throws ExcelExport.ExcelExportException {
77  renderSheet(sheet, style, 0, columnIndent, columns, data);
78 
79  // Resize all columns to fit the content size
80  for (int i = 0; i < columns.size(); i++) {
81  sheet.autoSizeColumn(i);
82  }
83 
84  // freeze header row
85  sheet.createFreezePane(0, 1);
86  }
87 
88  @Override
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());
93  }
94 
107  private static <T, C extends ExcelCellModel> int renderSheet(
108  Sheet sheet,
109  ExcelExport.WorksheetEnv worksheetEnv,
110  int rowStart,
111  int colStart,
112  List<ColumnModel<T, C>> columns, List<T> data)
114 
115  List<T> safeData = data == null ? Collections.emptyList() : data;
116  // Create a header row
117  Row headerRow = sheet.createRow(rowStart);
118  // Create header cells
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());
123  }
124 
125  // Create Cell Style for each column (if one is needed)
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 ExcelCellModel> colModel = columns.get(colNum);
131  ExcelCellModel cellModel = colModel.getCellRenderer().apply(rowData);
132  ExcelExport.createCell(worksheetEnv, row, colNum + colStart, cellModel, Optional.empty());
133  }
134  }
135 
136  return safeData.size() + 1;
137  }
138 }
ExcelTableExport(String sheetName, List< ColumnModel< T, C >> columns, List< T > data, int columnIndent)
static< T, CextendsExcelCellModel > int renderSheet(Sheet sheet, ExcelExport.WorksheetEnv worksheetEnv, int rowStart, int colStart, List< ColumnModel< T, C >> columns, List< T > data)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv style)
ExcelTableExport(String sheetName, List< ColumnModel< T, C >> columns, List< T > data)

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