java导出多个excel并打成zip包

这里主要是要针对数据量过大,通过生成多个excel文件并打成一个zip压缩包提供下载.

 

压缩excel的类  ZipUtil:

 

Java代码   收藏代码
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.util.zip.ZipOutputStream;  
  4. import java.util.zip.ZipEntry;  
  5. import java.util.zip.ZipFile;  
  6. import org.apache.commons.logging.Log;  
  7. import org.apache.commons.logging.LogFactory;  
  8.   
  9.   
  10. public class ZipUtil {  
  11.       
  12.     private static final Log log = LogFactory.getLog(ZipUtil.class);  
  13.   
  14.   
  15.     /** 
  16.      * 压缩文件 
  17.      *  
  18.      * @param srcfile File[] 需要压缩的文件列表 
  19.      * @param zipfile File 压缩后的文件 
  20.      */  
  21.     public static void zipFiles(List<File> srcfile, File zipfile) {  
  22.         byte[] buf = new byte[1024];  
  23.         try {  
  24.             // Create the ZIP file  
  25.             ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));  
  26.             // Compress the files  
  27.             for (int i = 0; i < srcfile.size(); i++) {  
  28.                 File file = srcfile.get(i);  
  29.                 FileInputStream in = new FileInputStream(file);  
  30.                 // Add ZIP entry to output stream.  
  31.                 out.putNextEntry(new ZipEntry(file.getName()));  
  32.                 // Transfer bytes from the file to the ZIP file  
  33.                 int len;  
  34.                 while ((len = in.read(buf)) > 0) {  
  35.                     out.write(buf, 0, len);  
  36.                 }  
  37.                 // Complete the entry  
  38.                 out.closeEntry();  
  39.                 in.close();  
  40.             }  
  41.             // Complete the ZIP file  
  42.             out.close();  
  43.         } catch (IOException e) {  
  44.            log.error("ZipUtil zipFiles exception:"+e);  
  45.         }  
  46.     }  
  47.   
  48.     /** 
  49.      * 解压缩 
  50.      *  
  51.      * @param zipfile File 需要解压缩的文件 
  52.      * @param descDir String 解压后的目标目录 
  53.      */  
  54.     public static void unZipFiles(File zipfile, String descDir) {  
  55.         try {  
  56.             // Open the ZIP file  
  57.             ZipFile zf = new ZipFile(zipfile);  
  58.             for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {  
  59.                 // Get the entry name  
  60.                 ZipEntry entry = ((ZipEntry) entries.nextElement());  
  61.                 String zipEntryName = entry.getName();  
  62.                 InputStream in = zf.getInputStream(entry);  
  63.                 // System.out.println(zipEntryName);  
  64.                 OutputStream out = new FileOutputStream(descDir + zipEntryName);  
  65.                 byte[] buf1 = new byte[1024];  
  66.                 int len;  
  67.                 while ((len = in.read(buf1)) > 0) {  
  68.                     out.write(buf1, 0, len);  
  69.                 }  
  70.                 // Close the file and stream  
  71.                 in.close();  
  72.                 out.close();  
  73.             }  
  74.         } catch (IOException e) {  
  75.             log.error("ZipUtil unZipFiles exception:"+e);  
  76.         }  
  77.     }  
  78.   
  79.     /** 
  80.      * Main 
  81.      *  
  82.      * @param args 
  83.      */  
  84.     public static void main(String[] args) {  
  85.         List<File> srcfile=new ArrayList<File>();  
  86.         srcfile.add(new File("e:\\1.xls"));  
  87.         srcfile.add(new File("e:\\2.xls"));  
  88.         srcfile.add(new File("e:\\3.xls"));  
  89.         srcfile.add(new File("e:\\4.xls"));  
  90.         srcfile.add(new File("e:\\5.xls"));  
  91.   
  92.         File zipfile = new File("e:\\edm.zip");  
  93.         ZipUtil.zipFiles(srcfile, zipfile);  
  94.     }  
  95. }  

 

 

生成多个excel 类:

 

Java代码   收藏代码
  1. /** 
  2.     * forward:导出Excel 
  3.     * @throws Exception 
  4.     */  
  5.    private ModelAndView forwardExport2Excel(HttpServletRequest request, HttpServletResponse response)  
  6.    throws Exception{  
  7.        try {  
  8.           
  9.             Integer page=0;//页数  
  10.             Integer pagesize=Integer.valueOf(PropertiesUtil.getPropertyValue("PAGESIZE"));//每页显示数  
  11.             Integer total=0;//总数  
  12.             int pageindex=1;//当前页数  
  13.             Integer rowfrom = 1;  
  14.             Integer rowto = 10;  
  15.             total =service.getTotal();  
  16.             if(total<pagesize){  
  17.                 page=1;  
  18.                 System.out.println("from:"+rowfrom+"---"+total);  
  19.                 List<Object> list = new ArrayList<Object>();  
  20.                 list = service.getAllList(rowfrom,total);  
  21.                 JXLUtil.writeEmailInfo2Excel(response, "邮箱信息总览""titles", list,pageindex);  
  22.             }else{                
  23.                 Integer mode=total%pagesize;  
  24.                 if(mode!=0){  
  25.                     page=total/pagesize+1;  
  26.                 }else{  
  27.                     page=total/pagesize;  
  28.                 }  
  29.                 if(total!=0 && page!=0){  
  30.                     while(pageindex<=page ){  
  31.                         if(total<0){  
  32.                             break;  
  33.                         }  
  34.                         if( total>pageindex*pagesize ){  
  35.                             System.out.println("from:"+(rowfrom)+"----"+pageindex*pagesize);  
  36.                             List<Object> list = new ArrayList<Object>();  
  37.                             list = service.getAllList(rowfrom,pagesize);  
  38.                             JXLUtil.writeEmailInfo2Excel(response, "邮箱信息总览""titles", list,pageindex);  
  39.                             rowfrom=pageindex*pagesize;  
  40.                             pageindex++;     
  41.                         }else{  
  42.                             System.out.println("from:"+rowfrom+"--"+total);  
  43.                             List<Object> list = new ArrayList<Object>();  
  44.                             list = service.getAllList(rowfrom,total);  
  45.                             JXLUtil.writeEmailInfo2Excel(response, "邮箱信息总览""titles", list,pageindex);  
  46.                             break;  
  47.                         }  
  48.                     }  
  49.                 }  
  50.         }     
  51.         List<File> srcfile=new ArrayList<File>();  
  52.         for(int p=1;p<=page;p++){  
  53.             srcfile.add(new File(PropertiesUtil.getPropertyValue("URL")+"zipname"+p+".xls"));  
  54.         }  
  55.         File zipfile = new File(PropertiesUtil.getPropertyValue("OUTPATH")+"zipname.zip");  
  56.            ZipUtil.zipFiles(srcfile, zipfile);  
  57.            String path = request.getContextPath();  
  58.            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";      
  59.         writeObject("成功导出excel  地址:<html><a href="+basePath+"zipname.zip>点击下载</a></html>", response);     
  60.        
  61.        } catch (Exception e) {  
  62.            log.error("error:"+e);  
  63.        }  
  64.        return ;  
  65.    }  

 jxl生成excel参考我之前的一篇即可

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值