| | |
| | | import org.dromara.common.core.utils.file.FileUtils; |
| | | import org.dromara.common.excel.convert.ExcelBigNumberConvert; |
| | | import org.dromara.common.excel.core.*; |
| | | import org.dromara.common.excel.handler.DataWriteHandler; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .registerWriteHandler(new DataWriteHandler(clazz)) |
| | | .sheet(sheetName); |
| | | if (merge) { |
| | | // 合并处理器 |
| | | builder.registerWriteHandler(new CellMergeStrategy(list, true)); |
| | | } |
| | | if (CollUtil.isNotEmpty(options)) { |
| | | // 添加下拉框操作 |
| | | builder.registerWriteHandler(new ExcelDownHandler(options)); |
| | | } |
| | | // 添加下拉框操作 |
| | | builder.registerWriteHandler(new ExcelDownHandler(options)); |
| | | builder.doWrite(list); |
| | | } |
| | | |
| | |
| | | * @param data 模板需要的数据 |
| | | * @param response 响应体 |
| | | */ |
| | | public static void exportTemplate(List<Object> data, String filename, String templatePath, HttpServletResponse response) { |
| | | public static <T> void exportTemplate(List<T> data, String filename, String templatePath, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(filename, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | |
| | | * @param data 模板需要的数据 |
| | | * @param os 输出流 |
| | | */ |
| | | public static void exportTemplate(List<Object> data, String templatePath, OutputStream os) { |
| | | public static <T> void exportTemplate(List<T> data, String templatePath, OutputStream os) { |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .registerWriteHandler(new DataWriteHandler(data.get(0).getClass())) |
| | | .build(); |
| | | WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | // 单表多数据导出 模板格式为 {.属性} |
| | | for (Object d : data) { |
| | | for (T d : data) { |
| | | excelWriter.fill(d, writeSheet); |
| | | } |
| | | excelWriter.finish(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 多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.属性} |
| | | * |
| | | * @param templatePath 模板路径 resource 目录下的路径包括模板文件名 |
| | |
| | | * @param os 输出流 |
| | | */ |
| | | public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) { |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | for (Map.Entry<String, Object> map : data.entrySet()) { |
| | | // 设置列表后续还有数据 |
| | | FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 多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) { |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("数据为空"); |
| | | } |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | | * 重置响应体 |
| | | */ |
| | | private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { |