• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
职业熬夜选手
博客园    首页    新随笔    联系   管理    订阅  订阅

java导出Excel(使用阿里巴巴的easyexcel)

1. 第一步导入maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.6</version>
</dependency>

2. 第二部创建数据模型(实体类)

@Data
@AllArgsConstructor
public class ExcelEntity {
    @ExcelProperty("姓名 (字符串)")
    private String name;
    @ExcelProperty("生日(时间)")
    private Date birthday;
    @ExcelProperty("年龄(数字)")
    private Integer age;
}

3. 第三步写Controller

@GetMapping("/getExcel")
public void getExcel(HttpServletResponse response) throws IOException {
    List<ExcelEntity> list =  new ArrayList<>(5);
    for (int i=1;i<=10; i++){
        list.add(new ExcelEntity("张"+i,new Date(),i));
    }
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("导出表格", "UTF-8");
    response.setHeader("Content-disposition", "attachment;fileName=" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), ExcelEntity.class).sheet("模板").doWrite(list);
}

4. 第四步测试

访问地址:http://localhost:8080/excel/getExcel

成功结果

有问题质询QQ:248048521,欢迎技术交流

 

posted @ 2021-05-19 15:47  职业熬夜选手  阅读(1485)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3