乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java導(dǎo)出多個(gè)excel并打成zip包

       瑤疏影 2016-08-04

      這里主要是要針對(duì)數(shù)據(jù)量過(guò)大,通過(guò)生成多個(gè)excel文件并打成一個(gè)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 解壓后的目標(biāo)目錄 
      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. }  

       

       

      生成多個(gè)excel 類:

       

      Java代碼  收藏代碼
      1. /** 
      2.     * forward:導(dǎo)出Excel 
      3.     * @throws Exception 
      4.     */  
      5.    private ModelAndView forwardExport2Excel(HttpServletRequest request, HttpServletResponse response)  
      6.    throws Exception{  
      7.        try {  
      8.           
      9.             Integer page=0;//頁(yè)數(shù)  
      10.             Integer pagesize=Integer.valueOf(PropertiesUtil.getPropertyValue("PAGESIZE"));//每頁(yè)顯示數(shù)  
      11.             Integer total=0;//總數(shù)  
      12.             int pageindex=1;//當(dāng)前頁(yè)數(shù)  
      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("成功導(dǎo)出excel  地址:<html><a href="+basePath+"zipname.zip>點(diǎn)擊下載</a></html>", response);     
      60.        
      61.        } catch (Exception e) {  
      62.            log.error("error:"+e);  
      63.        }  
      64.        return null;  
      65.    }  

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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多