Use DataToBeReplace.builder() to map enum/code values to display labels before passing content to ExcelExportManager.objectToExcelAndDownload()
When exporting domain objects to Excel, enum or code fields (e.g. status codes, boolean flags) must be displayed as human-readable labels. Embedding this mapping inside the DTO or service layer couples presentation concerns to business logic.
DataToBeReplace object that maps raw field values to display strings, keyed by DTO field name.ExcelExportManager.objectToExcelAndDownload(...).ResponseEntity<Resource> with a Content-Disposition: attachment header and an appropriate MIME type.// Controller method
@PostMapping("/items/list-filter-excel")
@FunctionId({FunctionIds.ITEM_EXCEL})
public ResponseEntity<Resource> downloadItemsExcel(
@RequestBody ExportParameterDto parameter) throws UnsupportedEncodingException {
// Force full-page query for export
parameter.getFiltersPageableDto().setPageNumber(1);
parameter.getFiltersPageableDto().setPagePerSize(Integer.MAX_VALUE);
Criteria criteria = CriteriaMakeHelper.INSTANCE
.gridFiltersToCriteria(parameter.getFiltersPageableDto().getGridFilters());
Page<ItemDto> items = itemService.findByCriteria(criteria,
parameter.getFiltersPageableDto().toPageable());
// Build enum-to-label replacement map
DataToBeReplace replaceData = DataToBeReplace.builder()
.replaceData("status", Map.of(
"ACTIVE", "Active",
"INACTIVE", "Inactive",
"PENDING", "Pending"
))
.replaceData("permission", Map.of(
Boolean.TRUE, "Allowed",
Boolean.FALSE, "Denied"
))
.build();
byte[] excelBytes = excelExportManager.objectToExcelAndDownload(
parameter.getGridColumnDataDto().getColumnDefs(), // List<ColumnDef>
items.getContent(), // List<?>
replaceData,
0, // sheet index
"yyyy-MM-dd HH:mm:ss" // date format
);
String encodedFilename = UriUtils.encode("items.xlsx", StandardCharsets.UTF_8);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename*=UTF-8''" + encodedFilename)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(new ByteArrayResource(excelBytes));
}
POST endpoint that downloads filtered data as an Excel file.pagePerSize = Integer.MAX_VALUE loads the entire result set into memory. Add a row-count guard or stream-based export for collections exceeding a few thousand rows.UriUtils.encode the filename for Content-Disposition to handle non-ASCII characters correctly across browsers.dateFormat parameter applies to all date fields in the sheet. Ensure the format string matches the locale expected by the consumer.grid-filter-to-criteria-converter — the same FiltersPageableDto is reused as the filter input for export endpoints.