0

I have to read and write Multiple excel files with POI and use ZipOutputStream to download. The code below archives file in D disk, but how do I use Apache POI to create and write excel files then archive it by 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();
}
  • You can just use the Apache POI to create Excel Files, then store the filenames in an `ArrayList` then loop the string as new files to be filled in the zipOutput – Ranielle Canlas Aug 28 '18 at 09:17
  • 1
    Is this a question about POI, or a question about zipping? –  Aug 28 '18 at 09:21
  • Look like you are trying to download the zip . where you are getting issue? – soorapadman Aug 28 '18 at 09:22
  • I suggest you take this problem one step at a time. First see how to generate an excel file, then how to create an archive with file(s). Then focus on the request to transfert the archive. For the moment, I am not sure where you are stuck so can't help more. – AxelH Aug 28 '18 at 09:27
  • @mrblewog i have example code to zip file but it zip file exist in D disk, but i want use POI to create new excel files(eg test.xls, test2.xls) and i want know how to zip test.xls, test2.xls – snorlax241 Aug 28 '18 at 09:29
  • I agree with @AxelH -- I'm not sure where we're at here. There is nothing special about zipping xls files, it works in the same way as zipping other files. –  Aug 28 '18 at 09:31
  • One thing I would suggest is creating a zip file on disk first, say D:\my-archive.zip, and then streaming that down in the response. Here's an answer about zipping multiple files into one zip: https://stackoverflow.com/questions/16546992/how-to-create-a-zip-file-of-multiple-image-files –  Aug 28 '18 at 09:32

0 Answers0