本文實例為大家分享了java導(dǎo)出百萬以上數(shù)據(jù)的excel文件,供大家參考,具體內(nèi)容如下
1.傳統(tǒng)的導(dǎo)出方式會消耗大量的內(nèi)存,2003每個sheet頁最多65536條數(shù)據(jù),2007每個sheet頁可以達(dá)到100萬條數(shù)據(jù)以上,2007會在生成Workbook時清理數(shù)據(jù),所以2007導(dǎo)出量更大;
2.可以導(dǎo)出多個excel文件到某個目錄中,然后打包下載;
3.導(dǎo)出excel格式的xml文件,這種方式可以分批導(dǎo)出數(shù)據(jù),適用于大批量數(shù)據(jù)的導(dǎo)出,以下簡單介紹這種方式:
代碼如下:
package com.epay.utils;/** * 大數(shù)據(jù)量導(dǎo)出成EXCEL或XML * @author qad * 2017-04-22 */import java.io.BufferedOutputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); try { DataOutputStream rafs = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(new File( "d://test.xls")))); sb.append("<?xml version=/"1.0/" encoding=/"GBK/" ?>"); sb.append("/n"); sb.append("<?mso-application progid=/"Excel.Sheet/"?>"); sb.append("/n"); sb.append("<Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/""); sb.append("/n"); sb.append(" xmlns:o=/"urn:schemas-microsoft-com:office:office/""); sb.append("/n"); sb.append(" xmlns:x=/"urn:schemas-microsoft-com:office:excel/""); sb.append("/n"); sb.append(" xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/""); sb.append("/n"); sb.append(" xmlns:html=/"http://www.w3.org/TR/REC-html40/">"); sb.append("/n"); sb.append("<DocumentProperties xmlns=/"urn:schemas-microsoft-com:office:office/"> "); sb.append("/n"); sb.append(" <Styles>/n"); sb.append(" <Style ss:ID=/"Default/" ss:Name=/"Normal/">/n"); sb.append(" <Alignment ss:Vertical=/"Center/"/>/n"); sb.append(" <Borders/>/n"); sb.append(" <Font ss:FontName=/"宋體/" x:CharSet=/"134/" ss:Size=/"12/"/>/n"); sb.append(" <Interior/>/n"); sb.append(" <NumberFormat/>/n"); sb.append(" <Protection/>/n"); sb.append(" </Style>/n"); sb.append(" </Styles>/n"); int sheetcount = 0; int recordcount = 65535; int currentRecord = 0; int total = 100000; int col = 20; sb.append("<Worksheet ss:Name=/"Sheet0/">"); sb.append("/n"); sb.append("<Table ss:ExpandedColumnCount=/"" + col + "/" ss:ExpandedRowCount=/"" + total + "/" x:FullColumns=/"1/" x:FullRows=/"1/">"); sb.append("/n"); for (int i = 0; i < total; i++) { if ((currentRecord == recordcount || currentRecord > recordcount || currentRecord == 0) && i != 0) {// 一個sheet寫滿 currentRecord = 0; rafs.write(sb.toString().getBytes()); sb.setLength(0); sb.append("</Table>"); sb.append("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">"); sb.append("/n"); sb.append("<ProtectObjects>False</ProtectObjects>"); sb.append("/n"); sb.append("<ProtectScenarios>False</ProtectScenarios>"); sb.append("/n"); sb.append("</WorksheetOptions>"); sb.append("/n"); sb.append("</Worksheet>"); sb.append("<Worksheet ss:Name=/"Sheet" + i / recordcount + "/">"); sb.append("/n"); sb.append("<Table ss:ExpandedColumnCount=/"" + col + "/" ss:ExpandedRowCount=/"" + recordcount + "/" x:FullColumns=/"1/" x:FullRows=/"1/">"); sb.append("/n"); } sb.append("<Row>"); for (int j = 0; j < col; j++) { System.out.println(i); sb.append("<Cell><Data ss:Type=/"String/">111</Data></Cell>"); sb.append("/n"); } sb.append("</Row>"); if (i % 5000 == 0) { rafs.write(sb.toString().getBytes()); rafs.flush(); sb.setLength(0); } sb.append("/n"); currentRecord++; } rafs.write(sb.toString().getBytes()); sb.setLength(0); sb.append("</Table>"); sb.append("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">"); sb.append("/n"); sb.append("<ProtectObjects>False</ProtectObjects>"); sb.append("/n"); sb.append("<ProtectScenarios>False</ProtectScenarios>"); sb.append("/n"); sb.append("</WorksheetOptions>"); sb.append("/n"); sb.append("</Worksheet>"); sb.append("</Workbook>"); sb.append("/n"); rafs.write(sb.toString().getBytes()); rafs.flush(); rafs.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}導(dǎo)出xml文件之后直接修改后綴名為.xlsx就可以直接打開,弊端:無法導(dǎo)出.xls格式的excel文件.以上只是一種簡單想法,留待以后具體實現(xiàn).
擴(kuò)展:既然可以使用xml文件導(dǎo)出excel,那么導(dǎo)出csv文件之后也可以直接改后綴名為.xls,目前只是一種想法,尚未證實可行性.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選