The main use is org.apache.tools.zip.ZipOutputStream this zip stream, here with Execl as an example.
The idea first writes the zip stream to the HTTP response output stream, and then writes the Excel stream to the zip stream (where you can simply read out the execl template, write the data output 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 voidExportall ()throwsioexception{Try{httpservletresponse response= This. GetResponse (); Response.setcontenttype ("Application/octet-stream"); String Execlname= "Report"; Response.AddHeader ("Content-disposition", "attachment;filename=" +NewString (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 BooleanExportall (OutputStream out) {zipoutputstream ZipStream=NULL; Hssfworkbook WB=NULL; Try{List<Test> datas =Testservice.gettestdata (); ZipStream=NewZipoutputstream (out);//This is the output of the zip stream to the HttpResponse stream for(inti=0;i<3;i++) {WB= Poiutil.getworkbook (i);//Get 0,1,2.xls fileHssfsheet sheet = wb.getsheetat (0); Testservice.setsheet (Sheet,datas);//... Handling File Content OperationsZipEntry ZipEntry =NewZipEntry (NewString ("File name xxx". GetBytes (), "utf-8") + ". xls"); Own name, here is assumed to be a zipstream.putnextentry (zipentry); Wb.write (ZipStream);//Here is the loop each time the EXECL is written to the ZIP packet Zipstream.flush ();
} }Catch(Exception e) {Throw Newsysexception (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 Newsysexception (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
Java batch file compression export and download to local