update 优化 ExcelUtil 工具支持合并处理器

2.X
疯狂的狮子li 3 years ago
parent 1003fb7511
commit 2aae9bc026

@ -2,10 +2,12 @@ package com.ruoyi.common.excel.utils;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; 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.StringUtils;
import com.ruoyi.common.core.utils.file.FileUtils; 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.DefaultExcelListener;
import com.ruoyi.common.excel.core.ExcelListener; import com.ruoyi.common.excel.core.ExcelListener;
import com.ruoyi.common.excel.core.ExcelResult; import com.ruoyi.common.excel.core.ExcelResult;
@ -69,22 +71,41 @@ public class ExcelUtil {
* *
* @param list * @param list
* @param sheetName * @param sheetName
* @return * @param clazz
* @param response
*/ */
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse 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 { try {
String filename = encodingFilename(sheetName); String filename = encodingFilename(sheetName);
response.reset(); response.reset();
FileUtils.setAttachmentResponseHeader(response, filename); FileUtils.setAttachmentResponseHeader(response, filename);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
ServletOutputStream os = response.getOutputStream(); ServletOutputStream os = response.getOutputStream();
EasyExcel.write(os, clazz) ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
.autoCloseStream(false) .autoCloseStream(false)
// 自动适配 // 自动适配
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
// 大数值自动转换 防止失真 // 大数值自动转换 防止失真
.registerConverter(new ExcelBigNumberConvert()) .registerConverter(new ExcelBigNumberConvert())
.sheet(sheetName).doWrite(list); .sheet(sheetName);
if (merge) {
// 合并处理器
builder.registerWriteHandler(new CellMergeStrategy(list, true));
}
builder.doWrite(list);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("导出Excel异常"); throw new RuntimeException("导出Excel异常");
} }

Loading…
Cancel
Save