一 基礎篇
1.座標:
<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>
2.controller
@RequestMapping(value = "/export", produces = {"application/json"}, method = RequestMethod.GET)
public void export(@Validated @RequestBody TrafficSearchVo detailSearchVo ,HttpServletResponse response) {
detailSearchVo.setProjectIdList(getAuthProjectIdList());
groupHomeTrafficService.export(detailSearchVo,response);
}
3.service
/**
* 導出excel
* @param detailSearchVo
* @param response
*/
public void export(TrafficSearchVo detailSearchVo,HttpServletResponse response){
//獲取數據庫數據
detailSearchVo.setItemsPerPage(Integer.MAX_VALUE);
Paginator<TrafficDataListVo> trafficDetailListPage = getTrafficDetailListPage(detailSearchVo);
List<TrafficDataListVo> list = Lists.newArrayList(trafficDetailListPage.getResults());
//生成Excel文件
ExportParams exportParams = new ExportParams();
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,
TrafficDataListVo.class,list);
workbook.setSheetName(0,"車流");
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
//通過輸出流輸出文件
try {
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode("車流", "UTF-8") + ".xls");
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (UnsupportedEncodingException e) {
log.error("======method:export exception:" ,e);
} catch (IOException e) {
log.error("======method:export exception:" ,e);
}
}
深入篇
1.標題單元格合併:關鍵參數 groupName
2.動態表頭
a.關鍵代碼(表頭):
ExcelExportEntity colEntity = new ExcelExportEntity("項目A", "projectName");
colEntity.setNeedMerge(true);
colEntity.setWidth(30);
colEntity.setHeight(20);
.....很多格式或姿勢你都可以在這裏設置
colList.add(colEntity);
...其他列省略
b.組裝數據:
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String,Object> h1 = new HashMap<String,Object>();
h1.put("projectName", "項目A");
h1.put("contractArea", 0);
h1.put("rentArea", 0);
h1.put("rentedSpaceNumber", 0);
h1.put("rentedArea", 0);
h1.put("rentedOccupancyRate", 0);
list.add(h1);
c.導出:
/**
* 生成excel (動態標題)
*
* @param response
* @param list 導出的數據
* @param headList excel 標題
* @param fileName 導出的文件名
*/
public static void generateExcelActiveHead(HttpServletResponse response, List<? extends Object> list, List<ExcelExportEntity> headList, String fileName) {
log.info("======method:generateExcelActiveHead list:{},", JSON.toJSONString(list));
//生成Excel文件
ExportParams exportParams = new ExportParams();
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, headList, list);
workbook.setSheetName(0, fileName);
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
//通過輸出流輸出文件
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (UnsupportedEncodingException e) {
log.error("======method:generateExcelActiveHead exception:", e);
} catch (IOException e) {
log.error("======method:generateExcelActiveHead exception:", e);
}
}
參考鏈接:
座標
代碼示例
@excel使用介紹
postman 如何下載eccel
標題合併
動態表頭
動態表頭