Penggunaan utama adalah zip stream org.apache.tools.zip.zipoutputStream. Di sini, execl digunakan sebagai contoh.
Ide pertama -tama tulis aliran zip ke dalam aliran output respons http, dan kemudian tulis aliran Excel ke aliran zip (Anda tidak perlu membuat file dan kemudian mengemasnya di sini. Cukup baca dan tulis templat execl dan output data ke dalam aliran zip, dan atur nama file untuk setiap aliran)
Misalnya: 1.xls, 2.xls, file 3.xls ada di file execl di bawah Webapp Proyek
1.Controller
@RequestMapping (value = "/exportAll", Method = requestMethod.get) public void exportAll () melempar ioException {coba {httpservletResponse response = this.getResponse (); response.setContentType ("Application/Octet-stream"); String execlname = "Laporan"; response.addheader ("disposisi konten", "lampiran; filename =" +string baru (execlname.getbytes (), "iso-8859-1") +". zip"); OutputStream out = response.getoutputStream (); testService.ExportAll (out); } catch (Exception e) {....}}2. Layanan
Impor java.io.outputStream; impor org.apache.poi.hssf.usermodel.hssfsheet; impor org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.tools.zip.zipentry; impor org.apache.apoolooPle.poolioP; java.io.fileInputStream; import javax.servlet.http.httpservletrequest; impor org.apache.poi.hssf.usermodel.hssfcellstyle; impor org.apache.poi.hssf.usermodel.hssfsheet; org.apache.poi.hssf.usermodel.hssfworkbook; impor org.apache.poi.poifs.filesystem.poifsfileSySySyStem; impor org.springframework.web.context.request.requestcontextholder; impor org.springframework.web.context.request.servletrequestattributes; public boolean exportAll (outputStream out) {zipoutputStream zipstream = null; Hssfworkbook wb = null; coba {list <est> 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); // ... menangani file konten file zipentry zipentry = new zipentry (string baru ("nama file xxx" .getbytes (), "UTF-8")+". XLS"); // Sebutkannya sendiri, inilah 1, 2, 3 zipstream.putNextEntry (zipentry); wb.write (zipstream); // Ini adalah loop yang menulis execl ke zippack setiap kali zipstream.flush (); }} catch (Exception e) {throw baru sysexception (errorConstants.common_system_error, e); } akhirnya {coba {if (wb! = null) {wb.close (); } if (zipstream! = null) {zipstream.close (); } out.flush (); out.close (); } catch (ioException e) {throw baru sysexception (errorConsants.common_close_error, e); }}} public static hssfworkbook getworkbook (string bh) {coba {string line = file.separator; ServletRequestAttributes AREQUESTATTRIBUTES = (servletRequestAttributes) requestContextholder.getRequestAttributes (); HttpservletRequest request = arequestAttributes == null? Null: ArequestAttributes.getRequest (); String webpath = request.getSerVletContext (). GetRealPath ("/"); File File = File Baru (WebPath+Line+"Excel"+Line+BH+". XLS"); Poifsfilesystem poifsfileSystem = poifsfileSystem baru (FileInputStream baru (file)); HSSFWorkbook WB = HSSFWorkbook baru (PoifsfileSystem); Return WB; } catch (Exception e) {throw baru sysexception (errorConstants.common_system_error, e); }}Hasil akhir menghasilkan laporan.zip, yang berisi 3 file: 1.xls, 2.xls, 3.xls
Meringkaskan
Di atas adalah ekspor kompresi file batch java dan unduh ke kode sampel lokal yang diperkenalkan oleh editor. Saya harap ini akan membantu semua orang. Jika Anda memiliki pertanyaan, silakan tinggalkan saya pesan dan editor akan membalas semua orang tepat waktu. Terima kasih banyak atas dukungan Anda ke situs web Wulin.com!