java匯出大批量(百萬以上)資料的excel檔案

來源:互聯網
上載者:User

標籤:www   write   false   center   book   cto   port   str   格式   

1.傳統的匯出方式會消耗大量的記憶體,2003每個sheet頁最多65536條資料,2007每個sheet頁可以達到100萬條資料以上,2007會在產生Workbook時清理資料,所以2007匯出量更大;

2.可以匯出多個excel檔案到某個目錄中,然後打包下載;

3.匯出excel格式的xml檔案,這種方式可以分批匯出資料,適用於大批量資料的匯出,以下簡單介紹這種方式:

代碼如下:

  1 package com.epay.utils;  2   3 /**  4  * 大資料量匯出成EXCEL或XML  5  * @author qad  6  * 2017-04-22  7  */  8 import java.io.BufferedOutputStream;  9 import java.io.DataOutputStream; 10 import java.io.File; 11 import java.io.FileNotFoundException; 12 import java.io.FileOutputStream; 13 import java.io.IOException; 14  15 public class Test { 16     public static void main(String[] args) { 17         StringBuffer sb = new StringBuffer(); 18         try { 19             DataOutputStream rafs = new DataOutputStream( 20                     new BufferedOutputStream(new FileOutputStream(new File( 21                             "d://test.xls")))); 22             sb.append("<?xml version=\"1.0\" encoding=\"GBK\" ?>"); 23             sb.append("\n"); 24             sb.append("<?mso-application progid=\"Excel.Sheet\"?>"); 25             sb.append("\n"); 26             sb.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""); 27             sb.append("\n"); 28             sb.append("  xmlns:o=\"urn:schemas-microsoft-com:office:office\""); 29             sb.append("\n"); 30             sb.append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\""); 31             sb.append("\n"); 32             sb.append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\""); 33             sb.append("\n"); 34             sb.append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"); 35             sb.append("\n"); 36             sb.append("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"> "); 37             sb.append("\n"); 38             sb.append(" <Styles>\n"); 39             sb.append("  <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n"); 40             sb.append("   <Alignment ss:Vertical=\"Center\"/>\n"); 41             sb.append("   <Borders/>\n"); 42             sb.append("   <Font ss:FontName=\"宋體\" x:CharSet=\"134\" ss:Size=\"12\"/>\n"); 43             sb.append("   <Interior/>\n"); 44             sb.append("   <NumberFormat/>\n"); 45             sb.append("   <Protection/>\n"); 46             sb.append("  </Style>\n"); 47             sb.append(" </Styles>\n"); 48             int sheetcount = 0; 49             int recordcount = 65535; 50             int currentRecord = 0; 51             int total = 100000; 52             int col = 20; 53             sb.append("<Worksheet ss:Name=\"Sheet0\">"); 54             sb.append("\n"); 55             sb.append("<Table ss:ExpandedColumnCount=\"" + col 56                     + "\" ss:ExpandedRowCount=\"" + total 57                     + "\" x:FullColumns=\"1\" x:FullRows=\"1\">"); 58             sb.append("\n"); 59             for (int i = 0; i < total; i++) { 60                 if ((currentRecord == recordcount 61                         || currentRecord > recordcount || currentRecord == 0) 62                         && i != 0) {// 一個sheet寫滿 63                     currentRecord = 0; 64                     rafs.write(sb.toString().getBytes()); 65                     sb.setLength(0); 66                     sb.append("</Table>"); 67                     sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); 68                     sb.append("\n"); 69                     sb.append("<ProtectObjects>False</ProtectObjects>"); 70                     sb.append("\n"); 71                     sb.append("<ProtectScenarios>False</ProtectScenarios>"); 72                     sb.append("\n"); 73                     sb.append("</WorksheetOptions>"); 74                     sb.append("\n"); 75                     sb.append("</Worksheet>"); 76                     sb.append("<Worksheet ss:Name=\"Sheet" + i / recordcount 77                             + "\">"); 78                     sb.append("\n"); 79                     sb.append("<Table ss:ExpandedColumnCount=\"" + col 80                             + "\" ss:ExpandedRowCount=\"" + recordcount 81                             + "\" x:FullColumns=\"1\" x:FullRows=\"1\">"); 82                     sb.append("\n"); 83                 } 84                 sb.append("<Row>"); 85                 for (int j = 0; j < col; j++) { 86                     System.out.println(i); 87                     sb.append("<Cell><Data ss:Type=\"String\">111</Data></Cell>"); 88                     sb.append("\n"); 89                 } 90                 sb.append("</Row>"); 91                 if (i % 5000 == 0) { 92                     rafs.write(sb.toString().getBytes()); 93                     rafs.flush(); 94                     sb.setLength(0); 95                 } 96                 sb.append("\n"); 97                 currentRecord++; 98             } 99             rafs.write(sb.toString().getBytes());100             sb.setLength(0);101             sb.append("</Table>");102             sb.append("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");103             sb.append("\n");104             sb.append("<ProtectObjects>False</ProtectObjects>");105             sb.append("\n");106             sb.append("<ProtectScenarios>False</ProtectScenarios>");107             sb.append("\n");108             sb.append("</WorksheetOptions>");109             sb.append("\n");110             sb.append("</Worksheet>");111             sb.append("</Workbook>");112             sb.append("\n");113             rafs.write(sb.toString().getBytes());114             rafs.flush();115             rafs.close();116         } catch (FileNotFoundException e) {117             e.printStackTrace();118         } catch (IOException e) {119             e.printStackTrace();120         }121     }122 }

匯出xml檔案之後直接修改尾碼名為.xlsx就可以直接開啟,弊端:無法匯出.xls格式的excel檔案.以上只是一種簡單想法,留待以後具體實現.

擴充:既然可以使用xml檔案匯出excel,那麼匯出csv檔案之後也可以直接改尾碼名為.xls,目前只是一種想法,尚未證實可行性.

 

java匯出大批量(百萬以上)資料的excel檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.