我必须使用POI读写多个excel文件,并使用ZipOutputStream进行下载。下面的代码将文件存档在D盘中,但是如何使用Apache POI创建和写入Excel文件,然后通过ZipOutputStream对其进行存档?
public void zipFiles(HttpServletResponse response, Workbook workbook, String fileName) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Content-Disposition", "attachment; filename=\""+ fileName +".zip\"");
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
// create a list to add files to be zipped
ArrayList<File> files = new ArrayList<>(2);
files.add(new File("D://兵庫_WEB申請書.xls"));
files.add(new File("D://兵庫_取消申請書.xls"));
// package files
for (File file : files) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fileInputStream = new FileInputStream(file);
IOUtils.copy(fileInputStream, zipOutputStream);
fileInputStream.close();
zipOutputStream.closeEntry();
}
zipOutputStream.close();
}