<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
由于不同版本会有不同的bug,本次操作所用版本3.2.0
//日经营数据导出
@PostMapping("/selectbyconditionExport")
public void selectbyconditionExport(@RequestBody DailyRevenueDto dailyRevenueDto, HttpServletResponse response) {
LocalDate today = DateTimeUtils.dateStrToLocalDate(dailyRevenueDto.getChargeTime());
LocalDate comperdate = DateTimeUtils.dateStrToLocalDate(dailyRevenueDto.getChargeTime2());
List<DailyRevenueVo> dailyRevenueVos = dailyRevenueService.selectbyconditionExport(dailyRevenueDto);//查出的数据
//模板的位置就在static文件夹下面 文件加载会报空指针这是第一个坑
TemplateExportParams params = new TemplateExportParams(
"日经营收入对比.xlsx");
// 重要代码
POICacheManager.setFileLoder(new FileLoaderImpl());
// sheet中要填充得数据
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = BeanUtils.convertListMap(dailyRevenueVos);//list转为map
int id=0;
for (Map<String, Object> stringObjectMap : list) {
id=id+1;
stringObjectMap.put("id",id);
}
map.put("maplist", list);
map.put("year", today.getYear());
map.put("month", today.getMonthValue());
map.put("day", today.getDayOfMonth());
map.put("xingqi", DateTimeUtils.getXingqi(today));
map.put("year1", comperdate.getYear());
map.put("month1", comperdate.getMonthValue());
map.put("day1", comperdate.getDayOfMonth());
map.put("xingqi1", DateTimeUtils.getXingqi(comperdate));
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
//根据表达式对表格设置颜色
EasyPoiUtils.setCellColor(workbook,s -> s.contains("-"),IndexedColors.RED.getIndex(),IndexedColors.YELLOW.getIndex());
EasyPoiUtils.downLoadExcel("日经营收入对比",response,workbook);
}
以上代码相关的类
package com.rfca.utils;
import cn.afterturn.easypoi.cache.manager.IFileLoader;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.sl