The main use is the zip stream org.apache.tools.zip.ZipOutputStream. Here, Execl is used as an example.
Idea First write the zip stream into the http response output stream, and then write the excel stream into the zip stream (you don’t need to generate files and then package them here. Just read and write the execl template and output the data into the zip stream, and set the file name for each stream)
For example: 1.xls, 2.xls, 3.xls files exist in the execl file under the 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"; 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 outputting 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);//... Handle file content operation ZipEntry zipEntry = new ZipEntry(new String("File name XXX".getBytes(),"utf-8")+".xls"); //Name it yourself, here is 1, 2, 3 zipStream.putNextEntry(zipEntry); wb.write(zipStream);//This is the loop that writes execl to the zippack 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 3 files: 1.xls, 2.xls, 3.xls
Summarize
The above is the Java batch file compression export and download to local sample code introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!