การใช้งานหลักคือ zip stream org.apache.tools.zip.zipoutputstream ที่นี่ Execl ถูกใช้เป็นตัวอย่าง
IDEA ก่อนเขียน zip stream ลงในสตรีมการตอบกลับ HTTP จากนั้นเขียนสตรีม Excel ลงใน zip stream (คุณไม่จำเป็นต้องสร้างไฟล์จากนั้นจัดทำแพ็คเกจที่นี่เพียงอ่านและเขียนเทมเพลต execl และส่งออกข้อมูลลงใน zip stream และตั้งชื่อไฟล์สำหรับแต่ละสตรีม)
ตัวอย่างเช่น: 1.xls, 2.xls, 3.xls มีอยู่ในไฟล์ execl ภายใต้ Project Webapp
1. เครื่องควบคุม
@RequestMapping (value = "/exportall", method = requestMethod.get) โมฆะสาธารณะ exportall () พ่น IOException {ลอง {httpservletResponse Response = this.getResponse (); Response.SetContentType ("แอปพลิเคชัน/octet-stream"); String exclName = "รายงาน"; Response.addHeader ("เนื้อหา-การจัดสรร", "attaintment; filename =" +สตริงใหม่ (ExeclName.getBytes (), "ISO-8859-1") +"zip"); outputStream out = response.getOutputStream (); testservice.exportall (ออก); } catch (Exception e) {.... }}2. บริการ
นำเข้า java.io.OutputStream; นำเข้า org.apache.poi.hssf.usermodel.hssfsheet; นำเข้า org.apache.poi.hssf.usermodel.hssfworkbook; นำเข้า org.apache.tools.zip.zipentry; java.io.fileinputstream; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า org.apache.poi.hssf.usermodel.hssfcellstyle; นำเข้า org.apache.poi.hsf.usermodel.hssfsheet; org.apache.poi.hssf.usermodel.hssfworkbook; นำเข้า org.apache.poi.poifs.filesystem.poifsfilesystem; นำเข้า org.springframework.web.context.request.requestcontextholder; org.springframework.web.context.request.servletrequestattributes; Public Boolean Exportall (OutputStream Out) {ZipOutputStream zipstream = null; hssfworkbook wb = null; ลอง {รายการ <Test> data = testService.getTestData (); zipstream = new ZipOutputStream (out); // ที่นี่คือการส่งสัญญาณซิปไปยังสตรีม httpresponse สำหรับ (int i = 0; i <3; i ++) {wb = poiutil.getworkbook (i); testService.SetSheet (แผ่นข้อมูล, data); // ... จัดการการทำงานเนื้อหาไฟล์ zipentry zipentry = ใหม่ zipentry (สตริงใหม่ ("ชื่อไฟล์ xxx" .getBytes (), "UTF-8")+". xls"); // ตั้งชื่อด้วยตัวคุณเองนี่คือ 1, 2, 3 zipstream.putnextentry (Zipentry); wb.write (zipstream); // นี่คือลูปที่เขียน execl ไปยัง zippack ทุกเวลา zipstream.flush (); }} catch (exception e) {โยน sysexception ใหม่ (errorConstants.Common_system_error, e); } ในที่สุด {ลอง {ถ้า (wb! = null) {wb.close (); } if (zipstream! = null) {zipstream.close (); } out.flush (); out.close (); } catch (ioexception e) {โยน sysexception ใหม่ (errorConstants.Common_close_error, e); }}} สาธารณะ hssfworkbook สาธารณะ getWorkbook (String bh) {ลอง {string line = file.separator; ServletRequestattributes arequestattributes = (servletrequestattributes) requestcontextholder.getRequestattributes (); httpservletrequest Request = arequestattributes == null? null: arequestattributes.getRequest (); String webPath = request.getServletContext (). getRealPath ("/"); ไฟล์ไฟล์ = ไฟล์ใหม่ (webPath+บรรทัด+"excel"+บรรทัด+bh+". xls"); poifsfilesystem poifsfilesystem = ใหม่ poifsfilesystem (ใหม่ FileInputStream (ไฟล์)); hssfworkbook wb = hssfworkbook ใหม่ (poifsfilesystem); กลับ WB; } catch (exception e) {โยน sysexception ใหม่ (errorConstants.Common_system_error, e); -ผลลัพธ์สุดท้ายจะสร้าง report.zip ซึ่งมี 3 ไฟล์: 1.xls, 2.xls, 3.xls
สรุป
ข้างต้นคือการส่งออกการบีบอัดไฟล์ Java Batch และดาวน์โหลดไปยังรหัสตัวอย่างท้องถิ่นที่แนะนำโดย Editor ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทุกคนในเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!