对EasyExcel写出流进行压缩
时间: 2023-10-13 10:08:11 浏览: 110
使用Java的ZipOutputStream类可以将流进行压缩。具体操作如下:
1. 创建ZipOutputStream对象,并将其绑定到压缩输出流上。
```
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
```
2. 将EasyExcel的输出流通过ZipEntry对象添加到ZipOutputStream中。
```
ZipEntry zipEntry = new ZipEntry("filename.xlsx");
zipOut.putNextEntry(zipEntry);
excelWriter.write(data, sheet);
excelWriter.finish();
```
3. 关闭ZipOutputStream。
```
zipOut.close();
```
完整代码如下:
```
public void compressExcel(List<MyData> data, OutputStream outputStream) {
try {
ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
Sheet sheet = new Sheet(1, 0, MyData.class);
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
ZipEntry zipEntry = new ZipEntry("filename.xlsx");
zipOut.putNextEntry(zipEntry);
excelWriter.write(data, sheet);
excelWriter.finish();
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
阅读全文