주된 용도는 Zip Stream org.apache.tools.zip.zipoutputstream입니다. 여기에서 EXECL은 예로 사용됩니다.
아이디어 먼저 Zip 스트림을 HTTP 응답 출력 스트림에 작성한 다음 Excel 스트림을 Zip 스트림에 씁니다 (파일을 생성 한 다음 여기에 포장 할 필요가 없습니다. EXECL 템플릿을 읽고 쓰고 데이터를 ZIP 스트림에 출력하십시오. 각 스트림의 파일 이름을 설정하십시오).
예를 들면 : 1.xls, 2.xls, 3.xls 파일은 프로젝트 웹 아프의 EXECL 파일에 존재합니다.
1. 콘트롤러
@requestmapping (value = "/exportall", method = recomport.get) public void exportall ()가 ioexception {httpservletresponse 응답 = this.getResponse (); Response.setContentType ( "Application/Octet-stream"); 문자열 execlname = "report"; response.addheader ( "content-disposition", "첨부 파일; filename =" +new String (execlname.getBytes (), "iso-8859-1") +". zip"); outputStream out = response.getOutputStream (); testservice.exportall (out); } catch (예외 E) {....}}2. 서비스
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.zipoutstream; 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; 가져 오기; org.springframework.web.context.request.servletrequestattributes; 공개 부울 ExportAll (outputStream out) {ZipoutputStream ZipStream = null; hssfworkbook wb = null; try {list <test> datas = testservice.getTestData (); zipStream = new ZipoutPutStream (out); // 여기에 zip 스트림을 HttPresponse 스트림 (int i = 0; i <3; i ++) {wb = poiutil.getworkbook (i); // get get hssfsheet sheet = wb.getSheetat (0); testservice.setsheet (시트, 데이터); // ... 파일 내용 핸들 핸들 파일 컨텐츠 작동 zipentry zipentry = new Zipentry (새 문자열 ( "파일 이름 xxx".getBytes (), "UTF-8")+". XLS"); // 여기에 직접 이름을 지정하십시오. wb.write (zipstream); // 이것은 zipstream.flush ()마다 execl을 zippack에 쓴 루프입니다. }} catch (예외 e) {새로운 sysexception 던지기 (ErrorConstants.common_system_error, e); } 마침내 {try {if (wb! = null) {wb.close (); } if (zipStream! = null) {zipStream.close (); } out.flush (); out.close (); } catch (ioException e) {새 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 (); 문자열 webpath = request.getServletContext (). getRealPath ( "/"); 파일 = 새 파일 (webpath+line+"Excel"+line+bh+". xls"); poifsfilesystem poifsfilesystem = new poifsfilesystem (new FileInputStream (file)); hssfworkbook wb = 새로운 hssfworkbook (poifsfilesystem); wb 리턴; } catch (예외 e) {throw new sysexception (ErrorConstants.common_system_error, e); }}최종 결과는 report.zip을 생성하며, 여기에는 1.xls, 2.xls, 3.xls의 3 개의 파일이 포함되어 있습니다.
요약
위는 Java 배치 파일 압축 내보내기 및 편집기가 소개 한 로컬 샘플 코드로 다운로드하는 것입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!