AutoPoi通过Map导出Excel
之前的代码是通过实体类来导出Excel的
JSONObject jsonObject = JSONObject.parseObject(data);
List<WaterMeterExport> list = service.listMeterWater(jsonObject);
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
// Step.3 AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
mv.addObject(NormalExcelConstants.FILE_NAME, "默认"); //此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(NormalExcelConstants.CLASS, WaterMeterExport.class);
//update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
ExportParams exportParams=new ExportParams("报表名称", "导出人:" + sysUser.getRealname());
exportParams.setImageBasePath(upLoadPath);
//update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
mv.addObject(NormalExcelConstants.PARAMS,exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, list);
return mv;
但是现在查询的参数是可变的,所以没办法使用实体类的方式来导出Excel,在查看AutoPoi的在线文档后,找到了用map的方式导出
代码部分
List<LinkedHashMap<String, Object>> tableMaps = (List<LinkedHashMap<String, Object>>) o;
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
// Step.3 AutoPoi 导出Excel
List<ExcelExportEntity> entityList = new ArrayList<>();
ModelAndView mv = new ModelAndView(new JeecgMapExcelView());
if (tableMaps.size()>0){
LinkedHashMap<String, Object> map = tableMaps.get(0);
for (Map.Entry<String, Object> entry : map.entrySet()) {
if ("f_collecttime".equals(entry.getKey())){
ExcelExportEntity entity = new ExcelExportEntity("时间" ,entry.getKey());
entity.setWidth(25);
entityList.add(entity);
}else {
ExcelExportEntity entity = new ExcelExportEntity(entry.getKey(), entry.getKey());
entity.setWidth(35);
entityList.add(entity);
}
}
}
//导出文件名称
mv.addObject(NormalExcelConstants.FILE_NAME, "默认");
mv.addObject(MapExcelConstants.ENTITY_LIST, entityList);
ExportParams exportParams=new ExportParams(title+"数据", "导出人:" + sysUser.getRealname());
exportParams.setImageBasePath(upLoadPath);
mv.addObject(NormalExcelConstants.PARAMS,exportParams);
mv.addObject(NormalExcelConstants.MAP_LIST, tableMaps);
return mv;