导出方法
This commit is contained in:
parent
66f1fb3da8
commit
156428784d
|
|
@ -223,6 +223,51 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
||||||
IoUtil.close(out);
|
IoUtil.close(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void downloadAndMergeExcel(List<Map<String, Object>> list, List<List<Integer>> mergeData, List<String> keyList, HttpServletResponse response) throws IOException {
|
||||||
|
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
|
||||||
|
|
||||||
|
// 创建工作簿
|
||||||
|
Workbook workbook = new XSSFWorkbook();
|
||||||
|
// 创建工作表
|
||||||
|
Sheet sheet = workbook.createSheet("Sheet1");
|
||||||
|
|
||||||
|
// 合并单元格,从第 0 行第 0 列到第 0 行第 2 列
|
||||||
|
// sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
|
||||||
|
|
||||||
|
for (List<Integer> mergeDatum : mergeData) {
|
||||||
|
sheet.addMergedRegion(new CellRangeAddress(mergeDatum.get(0), mergeDatum.get(1), mergeDatum.get(2), mergeDatum.get(3)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Row row0 = sheet.createRow(0);
|
||||||
|
for (int i = 0; i < keyList.size(); i++) {
|
||||||
|
Cell cell = row0.createCell(i);
|
||||||
|
cell.setCellValue(keyList.get(i));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
Map<String, Object> map = list.get(i);
|
||||||
|
Row row = sheet.createRow(i + 1);
|
||||||
|
for (int j = 0; j < keyList.size(); j++) {
|
||||||
|
Cell cell = row.createCell(j);
|
||||||
|
cell.setCellValue(map.get(keyList.get(j)) == null ? "" : map.get(keyList.get(j)).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// response为HttpServletResponse对象
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
|
||||||
|
|
||||||
|
try (FileOutputStream outputStream = new FileOutputStream(tempPath);
|
||||||
|
ServletOutputStream out = response.getOutputStream()) {
|
||||||
|
|
||||||
|
workbook.write(outputStream);
|
||||||
|
workbook.write(out);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// 更详细的错误处理
|
||||||
|
e.printStackTrace();
|
||||||
|
// 可以考虑返回一个错误响应给客户端
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 输入标题到excel
|
* 输入标题到excel
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue