servletsoutputstreamzipoutputstreamhssfworkbook

write large HSSFWorkbook into OutputStream with ZipOutputStream


Really appreciate the assistance since I am stuck here

I am Trying to download .xls file/files in zip format
Works fine when there are small amount of data like 150 records in xls file But once the data is huge it fails to download.

it redirects to a page which looks someting like this image

Here is my code

HSSFWorkbook workbook = null;
out = response.getOutputStream();   //response is of type HttpServletResponse , out is of type java.io.OutputStream
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out));
for(int i=0;i < selectedObjectsToDownload.length; i++){
//fetching data from DB
      workbook = new HSSFWorkbook();
      worksheet = workbook.createSheet("mySheet");
      workbook= getWorkbook(//data from DB);
      File fName=new File("File Path here");
      ZipEntry entry = new ZipEntry(fName.getName());
      zos.putNextEntry(entry);
      workbook.write(zos);
}

response.setContentType("application/zip");
response.setHeader("Content-Disposition","attachment; filename=\"" + "exported filename.zip" + "\"");
zos.close();
out.flush();
out.close();

Solution

  • Ok, I found the issue.

    Need to set character encoding before opening the workbook.

    Here is the modified code

    HSSFWorkbook workbook = null;
    out = response.getOutputStream();   //response is of type HttpServletResponse , out is of type java.io.OutputStream
    
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition","attachment; filename=\"" + "exported filename.zip" + "\"");
    **response.setCharacterEncoding("UTF-8");**
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out));
    for(int i=0;i < selectedObjectsToDownload.length; i++){
    //fetching data from DB
          workbook = new HSSFWorkbook();
          worksheet = workbook.createSheet("mySheet");
          workbook= getWorkbook(//data from DB);
          File fName=new File("File Path here");
          ZipEntry entry = new ZipEntry(fName.getName());
          zos.putNextEntry(entry);
          workbook.write(zos);
    }
    
    
    zos.close();
    out.flush();
    out.close();