java将HSSFWorkbook生成的excel压缩到zip中
思路:1.写入输入流中。
2.将输入流加到ZipOutputStream压缩流中
List<DocumentModel> list = null;
try {
list = documentService.exportDataList1(idsn);
} catch (Exception e) {
e.printStackTrace();
}
if(list==null||list.size()==0){
return ReponseResult.error(new CodeMsg(-1, "列表为空!"));
}
//excel表格
HSSFWorkbook wb = documentService.exportBatch1(list);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
//写入输入流
InputStream is = new ByteArrayInputStream(content);
// 创建临时文件
File zipFile = File.createTempFile(fileName, ".zip");
FileOutputStream f = new FileOutputStream(zipFile);
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
// 用于将数据压缩成Zip文件格式
ZipOutputStream zos = new ZipOutputStream(csum);
/**
* 添加excel表格数据
*/
zos.putNextEntry(new ZipEntry("file.xls"));
int bytesRead = 0;
while ((bytesRead = is.read()) != -1) {
zos.write(bytesRead);
}
isclose();
zos.closeEntry();