EasyExcel转化为byte[]、inputStream或者OutputStream

  • 场景:需要生成excel文件,并且存储到文件服务器

转为字节数组(生成excel)

public static byte[] ListToExcelOutPutStream(List<?> lst, Class clazz) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    ExcelWriter excelWriter  = EasyExcel.write(arrayOutputStream,clazz).build();
    WriteSheet writeSheet = EasyExcel.writerSheet().build();
    excelWriter.write(lst, writeSheet);
    excelWriter.finish();
    return arrayOutputStream.toByteArray();
}
  • 转为字节数组(模板填充)
        String templateFileName = this.getClass().getClassLoader().getResource("test.xlsx").getPath();
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap<>();
        map.put("name","zhanghao");
        map.put("number","18");
        mapList.add(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcel.write().withTemplate(templateFileName).file(os).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(mapList, writeSheet);
        excelWriter.finish();
        byte[] buffer = os.toByteArray();
  • 转为outputstream(模板填充)
        String templateFileName = this.getClass().getClassLoader().getResource("test.xlsx").getPath();
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap<>();
        map.put("name","zhanghao");
        map.put("number","18");
        mapList.add(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcel.write().withTemplate(templateFileName).file(os).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(mapList, writeSheet);
        excelWriter.finish();
        OutputStream outputStream =  excelWriter.writeContext().writeWorkbookHolder().getOutputStream();
  • 转为inputStream(模板填充)
        String templateFileName = this.getClass().getClassLoader().getResource("test.xlsx").getPath();
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap<>();
        map.put("name","zhanghao");
        map.put("number","18");
        mapList.add(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcel.write().withTemplate(templateFileName).file(os).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(mapList, writeSheet);
        excelWriter.finish();
        byte[] buffer = os.toByteArray();
        InputStream inputStream = new ByteArrayInputStream(buffer);
  • 总结

根据官方文档,正常情况下EasyExcel是将文件生成到本地,如果需要再操作,需要将文件转化为inputStream。
我们利用ByteArrayOutputStream为缓存,先将文件写入其中,然后将其转为字节数组,最后利用ByteArrayInputStream转为输入流,这样做省略了存储文件到本地的流程,省略了2个IO过程。
也可以转为输出流,做后续其他的流操作。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值