欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot easypoi导出excel

程序员文章站 2024-03-21 19:50:52
...
1.easypoi导出excel所需要的依赖包
<!--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.User实体类注解添加如下
@Entity
@Table(name = "t_user")
@ExcelTarget("user")
public class User {
  @Id
  @GeneratedValue
  @Excel(name = "编号", orderNum = "1", mergeVertical = true, isImportField = "id")
  private int id;
  @Excel(name = "姓名", orderNum = "2", mergeVertical = true, isImportField = "name")
  private String name;
3.UserController核心代码 
 @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 = userRepository.findAll();//获得用户
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);
    workbook.write(response.getOutputStream());
  }