| | |
| | | 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) { |
| | | // 合并处理器 |
| | |
| | | * @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(); |
| | |
| | | * @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(); |
| | |
| | | * @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()) |
| | |
| | | // 大数值自动转换 防止失真 |
| | | .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()) { |