|
|
|
@ -3,8 +3,8 @@ package org.dromara.common.excel.utils;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
import cn.hutool.core.io.resource.ClassPathResource;
|
|
|
|
import cn.hutool.core.io.resource.ClassPathResource;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
import cn.idev.excel.FastExcel;
|
|
|
|
|
|
|
|
import cn.idev.excel.ExcelWriter;
|
|
|
|
import cn.idev.excel.ExcelWriter;
|
|
|
|
|
|
|
|
import cn.idev.excel.FastExcel;
|
|
|
|
import cn.idev.excel.write.builder.ExcelWriterSheetBuilder;
|
|
|
|
import cn.idev.excel.write.builder.ExcelWriterSheetBuilder;
|
|
|
|
import cn.idev.excel.write.metadata.WriteSheet;
|
|
|
|
import cn.idev.excel.write.metadata.WriteSheet;
|
|
|
|
import cn.idev.excel.write.metadata.fill.FillConfig;
|
|
|
|
import cn.idev.excel.write.metadata.fill.FillConfig;
|
|
|
|
@ -476,4 +476,58 @@ public class ExcelUtil {
|
|
|
|
return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx";
|
|
|
|
return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 混合模式模板导出 (支持 Map 原地填充 + List 列表填充)
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param data 模板需要的数据
|
|
|
|
|
|
|
|
* @param filename 文件名
|
|
|
|
|
|
|
|
* @param templatePath 模板路径
|
|
|
|
|
|
|
|
* @param response 响应体
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void exportTemplateMixed(Map<String, Object> data, String filename, String templatePath, HttpServletResponse response) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
resetResponse(filename, response);
|
|
|
|
|
|
|
|
ServletOutputStream os = response.getOutputStream();
|
|
|
|
|
|
|
|
exportTemplateMixed(data, templatePath, os);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException("导出Excel异常");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 混合模式模板导出 (支持 Map 原地填充 + List 列表填充)
|
|
|
|
|
|
|
|
* Map/Object -> forceNewRow=false (原地替换占位符)
|
|
|
|
|
|
|
|
* Collection -> forceNewRow=true (展开列表)
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param data 模板需要的数据
|
|
|
|
|
|
|
|
* @param templatePath 模板路径
|
|
|
|
|
|
|
|
* @param os 输出流
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void exportTemplateMixed(Map<String, Object> data, String templatePath, OutputStream os) {
|
|
|
|
|
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
|
|
|
|
|
ExcelWriter excelWriter = FastExcel.write(os)
|
|
|
|
|
|
|
|
.withTemplate(templateResource.getStream())
|
|
|
|
|
|
|
|
.autoCloseStream(false)
|
|
|
|
|
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
WriteSheet writeSheet = FastExcel.writerSheet().build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
|
|
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
|
|
|
Object value = entry.getValue();
|
|
|
|
|
|
|
|
boolean isCollection = value instanceof Collection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 智能判断:只有集合才需要展开新行,普通数据原地替换
|
|
|
|
|
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(isCollection).build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isCollection) {
|
|
|
|
|
|
|
|
excelWriter.fill(new FillWrapper(key, (Collection<?>) value), fillConfig, writeSheet);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
excelWriter.fill(value, fillConfig, writeSheet);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
excelWriter.finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|