easyExcel导出文件为空解决

背景

今天遇到个要做导出的需求,很快就确定了技术方案用easyExcel,因为poi据说会有OOM问题,作为没有接触特别大型项目的我就不去深究这个问题了。在操作过程中发现了一个问题,直接拷用官方文档的文件导出的文件是空的,在一番研究之下写出了后面的代码,实现了用easyExcel的导出。直接上干货

包引入

这里说个贼有意思的事情,官方文档也是写的简介,简介到pom依赖都没有写。官方文档传送门:
https://www.yuque.com/easyexcel/doc/write

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

请求处理代码

	@ApiOperation(value = "xxxx", notes = "xxxxxx")
    @GetMapping("/xxxx")
    @SneakyThrows
    public void exportProductInfo(@RequestBody @Valid QualifiedProductCommand command, HttpServletResponse response){
        command.getPagination().setPageSize(5000);
        ExportDTO exportDTO = finishedGoodsService.qualifiedProduct(command).getData();
        String fileName = System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet =new WriteSheet();
        writeSheet.setHead(exportDTO.getTitle());
        writeSheet.setSheetName("xxx");
        excelWriter.write(exportDTO.getContent(),writeSheet);
        // 千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

此处对比官方文档的导出看:

@Test
    public void dynamicHeadWrite() {
        String fileName = TestFileUtil.getPath() + "dynamicHeadWrite" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName)
            // 这里放入动态头
            .head(head()).sheet("模板")
            // 当然这里数据也可以用 List<List<String>> 去传入
            .doWrite(data());
    }

当时看这官方代码有点蒙圈,也纳闷为啥流都没有处理还能导出呢。怀疑这是导出到磁盘文件的写法,没去测试过。用官方给出的方式导出文件是空的。加上**EasyExcel.write(response.getOutputStream())**后,便有流可以导出。

如有问题,希望大家评论区留言多多指教!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值