1、导出实体类列表
只需要在实体类上增加**@Excel(name = “姓名”)**
String fileName = "记录表";
ExportParams exportParams = new ExportParams("记录表","记录表");
Map map = new HashMap();
map.put(NormalExcelConstants.DATA_LIST,实体类list);
map.put(NormalExcelConstants.CLASS,类名.class);
map.put(NormalExcelConstants.PARAMS,exportParams);
map.put(NormalExcelConstants.FILE_NAME,fileName);
PoiBaseView.render(map,request,response,NormalExcelConstants.EASYPOI_EXCEL_VIEW);
注意这里使用的是NormalExcelConstants.EASYPOI_EXCEL_VIEW,需要指定实体类class;
2、导出Map列表(无实体类的数据,动态列的数据)
public void excelExport(HttpServletRequest request, HttpServletResponse response, @RequestBody *** ***) {
/*需要导出的原始数据*/
List<Map<String, String>> dataList = (List<Map<String, String>>) ***.get("data");
/*定义表头*/
List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();
/*生成动态表头*/
for (Map.Entry<String, String> entry : dataList.get(0).entrySet()) {
colEntity = new ExcelExportEntity(entry.getKey(),entry.getKey());
colEntity.setNeedMerge(true);
colList.add(colEntity);
}
}
Map map = new HashMap();
map.put(MapExcelConstants.ENTITY_LIST,colList);
map.put(MapExcelConstants.MAP_LIST,dataList);
map.put(MapExcelConstants.PARAMS,exportParams);
map.put(MapExcelConstants.FILE_NAME,fileName);
PoiBaseView.render(map,request,response,MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW)
}
这里使用的是MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW,需要生成ENTITY_LIST;
3、导出到本地文件
以上是通过http接口返回到前端,也可以导出到本地,不使用PoiBaseView.render
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("统计", "统计数据"), colList,dataList);
FileOutputStream fos = new FileOutputStream("D:/分析表.xls");
workbook.write(fos);
fos.close();
workbook.write也可以用于http接口如:
workbook.write(response.getOutputStream());
workbook.close();