add 新增 添加excel多sheet导出

2.X
疯狂的狮子Li 1 year ago
parent afcdabc12e
commit 838854ed83

@ -269,6 +269,26 @@ public class ExcelUtil {
} }
} }
/**
* sheet {key.}
*
* @param filename
* @param templatePath resource
* : excel/temp.xlsx
* : resource
* @param data
* @param response
*/
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, HttpServletResponse response) {
try {
resetResponse(filename, response);
ServletOutputStream os = response.getOutputStream();
exportTemplateMultiSheet(data, templatePath, os);
} catch (IOException e) {
throw new RuntimeException("导出Excel异常");
}
}
/** /**
* {key.} * {key.}
* *
@ -303,6 +323,42 @@ public class ExcelUtil {
excelWriter.finish(); excelWriter.finish();
} }
/**
* sheet {key.}
*
* @param templatePath resource
* : excel/temp.xlsx
* : resource
* @param data
* @param os
*/
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) {
ClassPathResource templateResource = new ClassPathResource(templatePath);
ExcelWriter excelWriter = EasyExcel.write(os)
.withTemplate(templateResource.getStream())
.autoCloseStream(false)
// 大数值自动转换 防止失真
.registerConverter(new ExcelBigNumberConvert())
.build();
if (CollUtil.isEmpty(data)) {
throw new IllegalArgumentException("数据为空");
}
for (int i = 0; i < data.size(); i++) {
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
for (Map.Entry<String, Object> map : data.get(i).entrySet()) {
// 设置列表后续还有数据
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
if (map.getValue() instanceof Collection) {
// 多表导出必须使用 FillWrapper
excelWriter.fill(new FillWrapper(map.getKey(), (Collection<?>) map.getValue()), fillConfig, writeSheet);
} else {
excelWriter.fill(map.getValue(), writeSheet);
}
}
}
excelWriter.finish();
}
/** /**
* *
*/ */

@ -94,6 +94,41 @@ public class TestExcelController {
exportExcelService.exportWithOptions(response); exportExcelService.exportWithOptions(response);
} }
/**
* sheet
*/
@GetMapping("/exportTemplateMultiSheet")
public void exportTemplateMultiSheet(HttpServletResponse response) {
List<TestObj1> list1 = new ArrayList<>();
list1.add(new TestObj1("list1测试1", "list1测试2", "list1测试3"));
list1.add(new TestObj1("list1测试4", "list1测试5", "list1测试6"));
List<TestObj1> list2 = new ArrayList<>();
list2.add(new TestObj1("list2测试1", "list2测试2", "list2测试3"));
list2.add(new TestObj1("list2测试4", "list2测试5", "list2测试6"));
List<TestObj1> list3 = new ArrayList<>();
list3.add(new TestObj1("list3测试1", "list3测试2", "list3测试3"));
list3.add(new TestObj1("list3测试4", "list3测试5", "list3测试6"));
List<TestObj1> list4 = new ArrayList<>();
list4.add(new TestObj1("list4测试1", "list4测试2", "list4测试3"));
list4.add(new TestObj1("list4测试4", "list4测试5", "list4测试6"));
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> sheetMap1 = new HashMap<>();
sheetMap1.put("data1", list1);
Map<String, Object> sheetMap2 = new HashMap<>();
sheetMap2.put("data2", list2);
Map<String, Object> sheetMap3 = new HashMap<>();
sheetMap3.put("data3", list3);
Map<String, Object> sheetMap4 = new HashMap<>();
sheetMap4.put("data4", list4);
list.add(sheetMap1);
list.add(sheetMap2);
list.add(sheetMap3);
list.add(sheetMap4);
ExcelUtil.exportTemplateMultiSheet(list, "多sheet列表", "excel/多sheet列表.xlsx", response);
}
/** /**
* *
*/ */

Loading…
Cancel
Save