List<Map<String, String>> dataSourceList = universalVO.getDataSourceList();
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet(universalVO.getChartTitle());
int columnIndex;
int rowIndex = 0;
for (Map<String, String> rowData : dataSourceList) {
Row dataRow = sheet.createRow(rowIndex++);
columnIndex = 0;
for (String key : rowData.keySet()) {
Cell cell = dataRow.createCell(columnIndex++);
cell.setCellValue(Objects.nonNull(rowData.get(key)) ? rowData.get(key) : “–”);
}
}
for (int i = 0; i < dataSourceList.get(0).size(); i++) {
sheet.autoSizeColumn(i);
}
int lastRowIndex = rowIndex - 1;
CellRangeAddress mergedRegion = new CellRangeAddress(lastRowIndex, lastRowIndex, 0, 2);
sheet.addMergedRegion(mergedRegion);
long start = System.currentTimeMillis();
try {
String encodeName = URLEncoder.encode(universalVO.getChartTitle() + “.xlsx”, StandardCharsets.UTF_8);
response.setHeader(
“Content-Disposition”,
“attachment; filename=”" + encodeName + “”; filename*=utf-8’'" + encodeName);
workbook.write(response.getOutputStream());
log.info(“数据详情写出成功,” + “耗时:” + (System.currentTimeMillis() - start));
} catch (IOException e) {
log.error(“背数据详情写出异常” + e.getMessage());
}
// 关闭workbook
try {
workbook.close();
} catch (IOException e) {
log.error(“数据详情写出异常” + e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
}
POI Excel导出
最新推荐文章于 2024-05-11 15:37:40 发布