浏览器调用接口,使用Hutool工具包的ExcelUtil导出xlsx类型的文件,返回给浏览器

pom依赖
<!-- hutool工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.6.3</version>
</dependency>

<!--poi对excel2007以上版本的支持-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
测试方法
@GetMapping("/test")
public void test(HttpServletResponse response) throws IOException {
    List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
    List<String> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1");
    List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2");
    List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
    List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");

    List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
    ExcelWriter writer = ExcelUtil.getWriter(true);
    writer.passCurrentRow();
    writer.merge(row1.size() - 1, "测试标题");
    writer.write(rows, true);
    String fileName = "Test.xlsx";
    response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
    // 定义下载的类型,标明是excel文件
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    OutputStream os = response.getOutputStream();
    writer.flush(os);
    os.flush();
    os.close();
    writer.close();
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值