Skip to content

Commit

Permalink
Add support for autosizing excel columns
Browse files Browse the repository at this point in the history
  • Loading branch information
agrancaric committed Jun 14, 2024
1 parent 2dc9620 commit 4c4a513
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public class CreateExcelReportRequest {
*/
private final MultiRowDataProvider multiRowDataProvider;

/**
* Determines if column size should be calculated based on content.
*/
private final boolean autoSizeColumns;

/**
* Creates {@link CreateExcelReportRequest} Builder instance from flat data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public class CreateReportGeneratorRequest {
*/
private final int firstRowIndex;

/**
* Determines if column size should be calculated based on content.
*/
private final boolean autoSizeColumns;

}
1 change: 1 addition & 0 deletions nrich-excel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public class ExampleReportService {
.batchSize(10)
.outputStream(outputStream)
.templatePath("classpath:excel/template.xlsx")
.autoSizeColumns(true)
.firstRowIndex(3)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.Assert;
Expand All @@ -54,24 +55,30 @@ public class PoiExcelReportGenerator implements ExcelReportGenerator {

private final SXSSFWorkbook workbook;

private final Sheet sheet;
private final SXSSFSheet sheet;

private final CreationHelper creationHelper;

private final Map<Integer, CellStyle> cellStyleMap;

private final Map<Class<?>, CellStyle> defaultStyleMap;

private final boolean autoSizeColumns;

private int currentRowNumber;

private boolean templateOpen = true;

public PoiExcelReportGenerator(List<CellValueConverter> cellValueConverterList, OutputStream outputStream, InputStream template, List<TemplateVariable> templateVariableList,
List<TypeDataFormat> typeDataFormatList, List<ColumnDataFormat> columnDataFormatList, int startIndex) {
List<TypeDataFormat> typeDataFormatList, List<ColumnDataFormat> columnDataFormatList, int startIndex, boolean autoSizeColumns) {
this.cellValueConverterList = cellValueConverterList;
this.outputStream = outputStream;
this.workbook = initializeWorkBookWithTemplate(template, templateVariableList);
this.sheet = workbook.getSheetAt(0);
this.autoSizeColumns = autoSizeColumns;
if (autoSizeColumns) {
this.sheet.trackAllColumnsForAutoSizing();
}
this.creationHelper = workbook.getCreationHelper();
this.cellStyleMap = createStyleMap(columnDataFormatList);
this.defaultStyleMap = createDefaultStyleMap(typeDataFormatList);
Expand All @@ -97,6 +104,7 @@ public void writeRowData(Object... reportDataList) {
@SneakyThrows
@Override
public void flush() {
autoSizeColumnsIfRequired();
workbook.write(outputStream);
this.templateOpen = false;
}
Expand Down Expand Up @@ -181,4 +189,11 @@ private Map<Class<?>, CellStyle> createDefaultStyleMap(List<TypeDataFormat> type
.filter(typeDataFormat -> typeDataFormat.getDataFormat() != null)
.collect(Collectors.toMap(TypeDataFormat::getType, value -> createCellStyle(value.getDataFormat())));
}

private void autoSizeColumnsIfRequired() {
if (autoSizeColumns) {
int numberOfColumns = this.sheet.getRow(this.sheet.getLastRowNum()).getLastCellNum();
IntStream.range(0, numberOfColumns + 1).forEach(sheet::autoSizeColumn);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public ExcelReportGenerator createReportGenerator(CreateReportGeneratorRequest r
InputStream template = resolveTemplate(request.getTemplatePath());

return new PoiExcelReportGenerator(
cellValueConverterList, request.getOutputStream(), template, request.getTemplateVariableList(), typeDataFormatList, request.getColumnDataFormatList(), request.getFirstRowIndex()
cellValueConverterList, request.getOutputStream(), template, request.getTemplateVariableList(), typeDataFormatList, request.getColumnDataFormatList(), request.getFirstRowIndex(),
request.isAutoSizeColumns()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private CreateReportGeneratorRequest toCreateReportGeneratorRequest(CreateExcelR
.outputStream(reportRequest.getOutputStream())
.templatePath(reportRequest.getTemplatePath())
.templateVariableList(reportRequest.getTemplateVariableList())
.autoSizeColumns(reportRequest.isAutoSizeColumns())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void setup() {
outputStream = new ByteArrayOutputStream();

excelReportGenerator = new PoiExcelReportGenerator(
cellValueConverterList, outputStream, template, templateVariableList, typeDataFormatList, columnDataFormatList, TEMPLATE_DATA_FIRST_ROW_INDEX
cellValueConverterList, outputStream, template, templateVariableList, typeDataFormatList, columnDataFormatList, TEMPLATE_DATA_FIRST_ROW_INDEX, false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static CreateExcelReportRequest createExcelReportRequest(Object[][] rowDa
.batchSize(batchSize)
.outputStream(outputStream)
.templatePath(TEMPLATE_PATH)
.autoSizeColumns(true)
.firstRowIndex(firstRowIndex).build();
}

Expand Down

0 comments on commit 4c4a513

Please sign in to comment.