EasyPoi的主要特點
1.設計精巧,使用簡單
2.接口豐富,擴展簡單
3.默認值多,write less do more
4.spring mvc支持,web導出可以簡單明瞭
公共方法導出數據到excel,共用查詢請求回顯對象,查詢方法,戴錶頭,可設置每列寬度。
導出格式如下
1,pom引入
<!-- Excel報表導出 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${easypoi.version}</version>
</dependency>
2,導出工具類
public class PoiUtils {
/**
* 動態表頭
*
* @param fileName
* @param list
* @param response
* @param title
* @param colList
*/
public static void exportExcelDynamicHeader(String fileName, List list, HttpServletResponse response, String title, List<ExcelExportEntity> colList) {
try {
response.setContentType("application/application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
if (list != null) {
ExportParams params = new ExportParams(title, "數據");
// 如果表頭長度大於256,修改成office 2007以上版本
if(colList.size() > 256){
params.setType(ExcelType.XSSF);
}
Workbook workbook = ExcelExportUtil.exportExcel(params, colList, list);
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
workbook.close();
outputStream.flush();// 刷新流
outputStream.close();// 關閉流
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 固定表頭數據導出
* @param fileName
* @param list
* @param response
* @param pojoClass
*/
public static void exportExcel(String fileName, List list, HttpServletResponse response, Class<?> pojoClass) {
try {
response.setContentType("application/application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
if (list != null) {
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, "sheet0"), pojoClass, list);
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
workbook.close();
outputStream.flush();// 刷新流
outputStream.close();// 關閉流
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3,查詢返回對象實體添加導出excel註解
@Data
@ExcelTarget("產品信息")
public class ExportProductExcel implements Serializable {
private static final long serialVersionUID = 212788451654665885L;
@Excel(name = "編碼", orderNum = "1", width = 20, needMerge = true)
private String code;
@Excel(name = "名稱", orderNum = "2", width = 56, needMerge = true)
private String name;
@Excel(name = "重量kg", orderNum = "3", width = 15, needMerge = true)
private Double weight;
@Excel(name = "體積m3", orderNum = "4", width = 15, needMerge = true)
private Double volume;
//報表隱藏字段或不導出的字段
private String factoryCode;
private String brandCode;
}
4,查詢和導出控制器
/**
* 分頁查詢產品信息
* @param productSelectQO
*/
@ResponseBody
@PostMapping("report")
public Result report(@RequestBody ProductSelectQO productSelectQO) {
PageHelper.startPage(productSelectQO.getPage(), productSelectQO.getPageSize());
List<ExportProductExcel> list = productManService.selectOfExcel(productSelectQO);
PageResultSet<ExportProductExcel> resultSet = PageResultSet.fromList(list);
return Result.success(resultSet);
}
/**
* 導出所有查詢的產品信息
* @param productSelectQO
* @param response
*/
@ResponseBody
@PostMapping("export")
public void export(@RequestBody ProductSelectQO productSelectQO,HttpServletResponse response) {
List<ExportProductExcel> list = productManService.selectOfExcel(productSelectQO);
PoiUtils.exportExcel("外購產品表" ,list,response, ExportProductExcel.class);
}