EasyExcel导出excel

//创建excel文件夹路径
String targetUrl = Constant.UPLOAD_TEMP + File.separator + DateUtil.getDate(“yyyyMMdd”);
File targetDir = new File(targetUrl);
if (!targetDir.exists()) {
targetDir.mkdirs();
}
String fileUrl = targetUrl + File.separator + “houan.xlsx”;
String excelName = “”;
InputStream resourceAsStream = null;
//判断日报表还是月报表
if (“day”.equals(data)) {
excelName = “后安日报表.xlsx”;
resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(“excel” + File.separator + “new.xlsx”);
} else {
excelName = “后安月报表.xlsx”;
resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(“excel” + File.separator + “month.xlsx”);
}
ExcelWriter excelWriter = EasyExcel.write(fileUrl)
.withTemplate(resourceAsStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
//填写列表信息
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
FillConfig fillConfigH = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
//获取时间
String date = parms.get(“time”);
String dateTime = DateUtil.getDate();
reportAlarm.setSeTime(date);
reportAlarm.setCuTime(dateTime);
//填写汇总信息
excelWriter.fill(new FillWrapper(“data1”, moniList), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper(“data2”, kgList), fillConfig, writeSheet);
excelWriter.fill(reportAlarm, fillConfigH, writeSheet);
excelWriter.finish();
//生成excel文件
String excelUrl = fileUrl;
//下载
File excel = new File(excelUrl);
if (!excel.exists()) {
throw new GlobalException(“文件不存在”);
}
byte[] bytes = FileUtils.readFileToByteArray(excel);
//中文名称乱码
excelName = new String(excelName.getBytes(“gbk”), “ISO8859-1”);
//加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
response.reset();
response.setHeader(“Content-Disposition”, “attachment; filename=” + excelName);
response.addHeader(“Content-Length”, “” + bytes.length);
response.setContentType(“application/octet-stream; charset=UTF-8”);
IOUtils.write(bytes, response.getOutputStream());
if (excel.exists()) {
excel.delete();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值