Java bulk file compression export and download to local

Mainly usedorg.apache.tools.zip . ZipOutputStream this zip stream, here's an example of Execl.

The idea is to first write the zip stream into the http response output stream, and then write the excel stream into the zip stream (here, you don't need to generate files and package them, just read and write the execl template and output the data to the zip stream, and set the file name for each stream)

For example: 1.xls, 2.xls, 3.xls file exists in execl file under project webapp

1.Controller

 @RequestMapping(value = "/exportAll",method = RequestMethod.GET)
    public void exportAll() throws IOException{
        try {
            HttpServletResponse response=this.getResponse();
            response.setContentType("application/octet-stream");
            String execlName = "Report form";
            response.addHeader("Content-Disposition", "attachment;filename="+new String(execlName.getBytes(),"iso-8859-1") +".zip");
            OutputStream out = response.getOutputStream();
            testService.exportAll(out);
        } catch (Exception e) {
            ....
        }
    }

2.Service

import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


public boolean exportAll(OutputStream out){ ZipOutputStream zipStream = null; HSSFWorkbook wb = null; try{ List<Test> datas = testService.getTestData(); zipStream = new ZipOutputStream(out);//Here is the output of the zip stream to the httpresponse stream for(int i=0;i<3;i++){ wb = POIUtil.getWorkbook(i);//Get 0,1,2.xls file HSSFSheet sheet = wb.getSheetAt(0); testService.setSheet(sheet,datas);//...Handling file content operations ZipEntry zipEntry = new ZipEntry(new String("file name XXX".getBytes(),"utf-8")+".xls"); //Name it yourself. Let's say it's 1, 2, 3 zipStream.putNextEntry(zipEntry); wb.write(zipStream);//This is the loop that writes execl to the zip package every time zipStream.flush();
     } }
catch (Exception e) { throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR, e); }finally { try { if(wb!=null){ wb.close(); } if(zipStream!=null){ zipStream.close(); } out.flush(); out.close(); } catch (IOException e) { throw new SysException(ERRORConstants.COMMON_CLOSE_ERROR, e); } } }

  public static HSSFWorkbook getWorkbook(String bh){
    try {
      String line = File.separator;
      ServletRequestAttributes aRequestAttributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
      HttpServletRequest request =aRequestAttributes==null?null:aRequestAttributes.getRequest();
      String webpath=request.getServletContext().getRealPath("/");
      File file = new File(webpath+line+"excel"+line+bh+".xls");

      POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
      HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
      return wb;
   } catch (Exception e) {
      throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR,e);
   }
  }


The final result generates a report. zip, which contains three files: 1.xls,2.xls,3.xls

Tags: Java Apache Excel

Posted on Sat, 02 May 2020 22:11:05 -0700 by synchro_irl