easypoi-springboot/springmvc导出数据为Excel

1 篇文章 0 订阅
1 篇文章 0 订阅

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等各种格式.

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值