From 2aae9bc026714ef3d05628d66986e1674e34eb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 31 May 2022 13:40:05 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20ExcelUtil=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=94=AF=E6=8C=81=E5=90=88=E5=B9=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/excel/utils/ExcelUtil.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/utils/ExcelUtil.java index f1f39fc4..450dc6b8 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/utils/ExcelUtil.java @@ -2,10 +2,12 @@ package com.ruoyi.common.excel.utils; import cn.hutool.core.util.IdUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; -import com.ruoyi.common.excel.convert.ExcelBigNumberConvert; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.file.FileUtils; +import com.ruoyi.common.excel.convert.ExcelBigNumberConvert; +import com.ruoyi.common.excel.core.CellMergeStrategy; import com.ruoyi.common.excel.core.DefaultExcelListener; import com.ruoyi.common.excel.core.ExcelListener; import com.ruoyi.common.excel.core.ExcelResult; @@ -69,22 +71,41 @@ public class ExcelUtil { * * @param list 导出数据集合 * @param sheetName 工作表的名称 - * @return 结果 + * @param clazz 实体类 + * @param response 响应体 */ public static void exportExcel(List list, String sheetName, Class clazz, HttpServletResponse response) { + exportExcel(list, sheetName, clazz, false, response); + } + + /** + * 导出excel + * + * @param list 导出数据集合 + * @param sheetName 工作表的名称 + * @param clazz 实体类 + * @param merge 是否合并单元格 + * @param response 响应体 + */ + public static void exportExcel(List list, String sheetName, Class clazz, boolean merge, HttpServletResponse response) { try { String filename = encodingFilename(sheetName); response.reset(); FileUtils.setAttachmentResponseHeader(response, filename); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); ServletOutputStream os = response.getOutputStream(); - EasyExcel.write(os, clazz) + ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) .autoCloseStream(false) // 自动适配 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 大数值自动转换 防止失真 .registerConverter(new ExcelBigNumberConvert()) - .sheet(sheetName).doWrite(list); + .sheet(sheetName); + if (merge) { + // 合并处理器 + builder.registerWriteHandler(new CellMergeStrategy(list, true)); + } + builder.doWrite(list); } catch (IOException e) { throw new RuntimeException("导出Excel异常"); }