公共方法导出数据到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);
}