easypoi-springboot/springmvc导出数据为Excel

来源:互联网 发布:杭州市大数据管理局 编辑:程序博客网 时间:2024/06/07 19:56

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法

官方资料

http://git.oschina.net/jueyue/easypoi

1.Maven中添加依赖

        <!--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>

2 POJO中添加注解

@Entity@Table(name = "user")@ExcelTarget("user")public class User {  @Id  @GeneratedValue  @Excel(name = "编号", orderNum = "1", mergeVertical = true, isImportField = "id")  private Long id;  @Excel(name = "姓名", orderNum = "2", mergeVertical = true, isImportField = "name")  private String name;  @Excel(name = "年龄", orderNum = "3", mergeVertical = true, isImportField = "age")

3 Controller中请求

// 下载execl文档  @RequestMapping("/downloadExcel")  public void download(HttpServletRequest request, HttpServletResponse response) 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<User> list = userService.findAll();    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);    workbook.write(response.getOutputStream());  }

4 前端页面添加Button

<input type="button" value="导出为Excel" onclick="window.open('/downloadExcel');"/>

关于更多easypoi的注解和功能,请参看http://git.oschina.net/jueyue/easypoi ,easypoi对springmvc支持很好!可以导出数据为word/excel/pdf等各种格式.

原创粉丝点击