mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 ExcelUtil 工具支持合并处理器
This commit is contained in:
		@@ -2,8 +2,10 @@ package com.ruoyi.common.utils.poi;
 | 
			
		||||
 | 
			
		||||
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.convert.ExcelBigNumberConvert;
 | 
			
		||||
import com.ruoyi.common.excel.CellMergeStrategy;
 | 
			
		||||
import com.ruoyi.common.excel.DefaultExcelListener;
 | 
			
		||||
import com.ruoyi.common.excel.ExcelListener;
 | 
			
		||||
import com.ruoyi.common.excel.ExcelResult;
 | 
			
		||||
@@ -69,22 +71,41 @@ public class ExcelUtil {
 | 
			
		||||
     *
 | 
			
		||||
     * @param list      导出数据集合
 | 
			
		||||
     * @param sheetName 工作表的名称
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     * @param clazz     实体类
 | 
			
		||||
     * @param response  响应体
 | 
			
		||||
     */
 | 
			
		||||
    public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
 | 
			
		||||
        exportExcel(list, sheetName, clazz, false, response);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 导出excel
 | 
			
		||||
     *
 | 
			
		||||
     * @param list      导出数据集合
 | 
			
		||||
     * @param sheetName 工作表的名称
 | 
			
		||||
     * @param clazz     实体类
 | 
			
		||||
     * @param merge     是否合并单元格
 | 
			
		||||
     * @param response  响应体
 | 
			
		||||
     */
 | 
			
		||||
    public static <T> void exportExcel(List<T> list, String sheetName, Class<T> 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异常");
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user