使用easypoi导出excel
easypoi在项目中的应用:
需要引入的jar包:
<!--easypoi导出excel-->
<!--easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能-->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-base</artifactId>
<version>2.3.1</version>
</dependency>
<!--easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能-->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-web</artifactId>
<version>2.3.1</version>
</dependency>
<!--easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理-->
<dependency>
<groupId>org.jeecg</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>2.3.1</version>
</dependency>
然后在实体类中添加easypoi的注解:
@ExcelTarget("summaryexcel")
@Data
public class SummaryExcel {
@Excel(name = "日期", orderNum = "1", mergeVertical = true, isImportField = "date")
private String date;//当天日期
@Excel(name = "能源类型", orderNum = "2", mergeVertical = true, isImportField = "type")
private String type; // 能源类型
@Excel(name = "能源用量", orderNum = "3", mergeVertical = true, isImportField = "sum")
private Double sum; // 用量
}
然后就可以在controller层直接使用easypoi:
@GetMapping("getexcel")
public void download(HttpServletRequest request, HttpServletResponse response,String start,String end) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("数据表","UTF-8") + ".xls");
//编码
response.setCharacterEncoding("UTF-8");
List<SummaryExcel> list = summaryService.summaryexcel(start,end);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), SummaryExcel.class, list);
workbook.write(response.getOutputStream());
}
easypoi在开源中国: https://www.oschina.net/news/54995/jeecg-easypoi-2-0-3
easypoi文档: http://easypoi.mydoc.io/