leeqianjun 发表于 2013-2-7 09:42:32

构造Excel文件并进行压缩下载

一.需求的来源
 
          有个产品有个数据导出的需求,需要把产品、订单等数据导出。仔细的想了一下,决定采用先把数据库的数据生成Excel,然后把多个Excel文件组装到ZIP压缩文件中,然后输出。
 
二.实现方式
  
public void writeZipFile() throws Exception{System.out.println(" #### writeZipFile ####");String zipFile = "C:/out.zip";File file = new File(zipFile);ZipOutputStream zipOutput=new ZipOutputStream(new FileOutputStream(file)); zipOutput.setMethod(ZipOutputStream.DEFLATED); DataOutputStream os=new DataOutputStream(zipOutput); int count = 1000;for(int i=0;i<count;i++){String zipName = "CodeReview_" + i + ".xls";zipOutput.putNextEntry(new ZipEntry(zipName)); FileInputStream is = new FileInputStream(new File("c:/CodeReview.xls"));   byte[] buf = new byte;    while(is.read(buf) > 0){os.write(buf);}}os.close(); System.out.println(" #### 共写入:" + count + "个Excel文件####");}  
三.结论
   输入是:1000个  43k 的Excel文件
  输出是: 5387K ZIP文件
   压缩比达到 8左右
页: [1]
查看完整版本: 构造Excel文件并进行压缩下载