| | |
| | | import com.alibaba.excel.write.metadata.fill.FillConfig; |
| | | import com.alibaba.excel.write.metadata.fill.FillWrapper; |
| | | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.file.FileUtils; |
| | | import org.dromara.common.excel.convert.ExcelBigNumberConvert; |
| | | import org.dromara.common.excel.core.CellMergeStrategy; |
| | | import org.dromara.common.excel.core.DefaultExcelListener; |
| | | import org.dromara.common.excel.core.ExcelListener; |
| | | import org.dromara.common.excel.core.ExcelResult; |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | 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; |
| | | import java.io.OutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, false, os); |
| | | exportExcel(list, sheetName, clazz, false, os, null); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param response 响应体 |
| | | * @param options 级联下拉选 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, false, os, options); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, merge, os); |
| | | exportExcel(list, sheetName, clazz, merge, os, null); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param merge 是否合并单元格 |
| | | * @param response 响应体 |
| | | * @param options 级联下拉选 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) { |
| | | try { |
| | | resetResponse(sheetName, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportExcel(list, sheetName, clazz, merge, os, options); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出Excel异常"); |
| | | } |
| | |
| | | * @param os 输出流 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os) { |
| | | exportExcel(list, sheetName, clazz, false, os); |
| | | exportExcel(list, sheetName, clazz, false, os, null); |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param os 输出流 |
| | | * @param options 级联下拉选内容 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os, List<DropDownOptions> options) { |
| | | exportExcel(list, sheetName, clazz, false, os, options); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param merge 是否合并单元格 |
| | | * @param os 输出流 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, OutputStream os) { |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, |
| | | OutputStream os, List<DropDownOptions> options) { |
| | | ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) |
| | | .autoCloseStream(false) |
| | | // 自动适配 |
| | | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .registerWriteHandler(new DataWriteHandler(clazz)) |
| | | .sheet(sheetName); |
| | | if (merge) { |
| | | // 合并处理器 |
| | | builder.registerWriteHandler(new CellMergeStrategy(list, true)); |
| | | } |
| | | // 添加下拉框操作 |
| | | 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) { |
| | | private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { |
| | | String filename = encodingFilename(sheetName); |
| | | FileUtils.setAttachmentResponseHeader(response, filename); |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); |
| | |
| | | if (StringUtils.containsAny(propertyValue, separator)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[0].equals(value)) { |
| | | propertyString.append(itemArray[1]).append(separator); |
| | | propertyString.append(itemArray[1] + separator); |
| | | break; |
| | | } |
| | | } |
| | |
| | | if (StringUtils.containsAny(propertyValue, separator)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[1].equals(value)) { |
| | | propertyString.append(itemArray[0]).append(separator); |
| | | propertyString.append(itemArray[0] + separator); |
| | | break; |
| | | } |
| | | } |