
java easyexcel导出excel文件oom,但是easyexcel底层用不到这两个吧,哪里占用的呢
public void exportDetailInfoList(@RequestBody Map params,HttpServletResponse response,UserInfo userInfo) throws IOException {
log.info("-----------缴税详细信息导出: " + JsonUtils.beanToJson(params));
try {
Map<String, Object> oldParams = new HashMap<>(params);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String excelName = URLEncoder.encode("缴税详细信息", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + excelName + ".xlsx");
long start = System.currentTimeMillis();
InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream("template/详细信息查询导出模板,表头格式.xlsx");
//设置输出流和模板信息
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).useDefaultStyle(false).withTemplate(templateInputStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
//开启自动换行,自动换行表示每次写入一条list数据是都会重新生成一行空行,此选项默认是关闭的,需要提前设置为true
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
Map<String, Object> query = new HashMap<>();
params.put("offset",1);
params.put("limit",1);//只为获取总条数
query = taxService.queryDetailInfoList(params,userInfo.getUser());
Integer totalNum = Integer.parseInt(query.get("total").toString()) ;
List<TaxDetailInfoExcel> firstData = JsonUtils.beanToArray(query.get("rows"), TaxDetailInfoExcel.class);
int total = totalNum.intValue();
int limit = 10000;
int times = (total + limit - 1)/limit;
Integer num = 1;
for (int i = 1; i <= times; i++) {
params.putAll(oldParams);
params.put("offset",i);
params.put("limit",limit);
params.put("notCount","1");
//todo 不要每次都查总
query = taxService.queryDetailInfoList(params,userInfo.getUser());
List<TaxDetailInfoExcel> subList = JsonUtils.beanToArray(query.get("rows"), TaxDetailInfoExcel.class);
for (TaxDetailInfoExcel taxDetailInfoExcel : subList) {
taxDetailInfoExcel.setNum((num++).toString());
}
long list1 = System.currentTimeMillis();
FillWrapper fillWrapper1 = new FillWrapper("detail",subList);
// 打印日志以确认进度
log.info("Processing batch: " + i);
excelWriter.fill(fillWrapper1,fillConfig,writeSheet);
subList.clear(); // 清除内存中的数据,避免内存膨胀
long list2 = System.currentTimeMillis();
log.info("list2-list1:" + (list2 - list1) + "ms");
}
params.putAll(oldParams);
params.remove("notCount");
Map<String, Object> totalMap = taxService.queryDetailInfoTotal(params, userInfo.getUser());
List<TaxDetailInfoExcel> totalData = JsonUtils.beanToArray(totalMap.get("rows"), TaxDetailInfoExcel.class);
FillWrapper fillWrapper2 = new FillWrapper("total",totalData);
excelWriter.fill(fillWrapper2,writeSheet);
//map的key就是excel的{key}
String createTime = (String) oldParams.get("between_b_printDate");
String tradeTaxNo = "";
if(!com.easipass.utils.StringUtils.isEmpty(params.get("like_a_tradeTaxNo")) && CollectionUtils.isNotEmpty(firstData)){
TaxDetailInfoExcel taxDetailInfoExcel = firstData.get(0);
String like_a_tradeTaxNo = taxDetailInfoExcel.getTradeTaxNo();
if (like_a_tradeTaxNo != null && like_a_tradeTaxNo.length() > 0) {
// 如果长度大于5,截取0-5个字符
if (like_a_tradeTaxNo.length() > 5) {
like_a_tradeTaxNo = like_a_tradeTaxNo.substring(0, 5);
}
// 如果长度小于5,截取所有字符
else {
like_a_tradeTaxNo = like_a_tradeTaxNo.substring(0);
}
}
tradeTaxNo = like_a_tradeTaxNo;
}
String[] split = createTime.split(",");
Map<String, Object> map = new HashMap<>();
map.put("startDate",split[0]);
map.put("endDate",split[1]);
map.put("tradeTaxNo",tradeTaxNo);
map.put("fillDate", DateUtils.getDateString("yyyy年MM月"));
excelWriter.fill(map,writeSheet);
excelWriter.finish();
long end = System.currentTimeMillis();
log.info("导出缴税详细信息耗时:" + (end - start) + "ms");
} catch (Exception e) {
log.info("缴税详细信息导出失败 ");
log.info(e.getMessage(), e);
throw e;
}
}