xml方式excel、以及與jxl方式匯出效能比較
阿新 • • 發佈:2018-12-12
通過xml方式匯出excel沒有65536資料條數限制,於是嘗試了一下,具體程式碼如下:
private static int maxRow = 1000000; /** * 通過xml方式寫入文件匯出excel檔案 * @param sheetName * @param sheetData * @return * @throws Exception */ public static StringBuffer exportExcel(String sheetName,ExcelSheetData sheetData) throws Exception{ // 建立一個excel應用檔案 StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"); 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"); try { // 生成表格 int headersLength = sheetData.getTitleList().size(); sb.append("<Worksheet ss:Name=\"" + sheetName + "\">"); // sb.append("\n"); sb.append("<Table ss:ExpandedColumnCount=\"" + headersLength + "\" ss:ExpandedRowCount=\""+maxRow+"\" x:FullColumns=\"1\" x:FullRows=\"1\">"); // sb.append("\n"); sb.append("<Row>"); //標題 for(int i=0;i<sheetData.getTitleList().size();i++){ sb.append("<Cell><Data ss:Type=\"String\">"+sheetData.getTitleList().get(i).getName()+"</Data></Cell>"); // sb.append("\n"); } sb.append("</Row>"); // sb.append("\n"); //匯入資料 for(int i=0;i<sheetData.getDataMapList().size();i++){ sb.append("<Row>"); for (Map.Entry<Integer,Object> dataMap : sheetData.getDataMapList().get(i).entrySet()) { sb.append("<Cell><Data ss:Type=\"String\">"+dataMap.getValue()+"</Data></Cell>"); // sb.append("\n"); } sb.append("</Row>"); // sb.append("\n"); } 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"); } catch (Exception e) { e.printStackTrace(); } return sb; }
測試方法如下:
public static void main(String[] args) throws Exception { String filename = "test"; List<ExcelTitleData> titleList = new ArrayList<ExcelTitleData>(); titleList.add(new ExcelTitleData("編號",10)); titleList.add(new ExcelTitleData("賬號",20)); titleList.add(new ExcelTitleData("密碼",30)); titleList.add(new ExcelTitleData("交易碼",10)); String sheetName ="使用者資訊"; List<Map<Integer, Object>> dataList = new ArrayList<Map<Integer, Object>>(); Map<Integer, Object> map = new HashMap<Integer, Object>(); map.put(1,222); map.put(2,"wdwd"); map.put(3,789); map.put(4,"Dqwqwe"); for (int i = 0; i < 10 ; i++) { dataList.add(map); } ExcelSheetData sheetData = new ExcelSheetData(sheetName,titleList,dataList); StringBuffer sb = exportExcel(filename,sheetData); long start = System.currentTimeMillis(); outputLocalXls(sb,filename,"E://1/"); long end = System.currentTimeMillis(); System.out.println(end-start); } public static void outputLocalXls(StringBuffer sb, String fileName, String path) { try { FileOutputStream fos = null; try { File f = new File(path+fileName +".xls"); if (!f.getParentFile().exists()) { //判斷父目錄路徑是否存在,即test.txt前的I:\a\b\ try { f.getParentFile().mkdirs(); //不存在則建立父目錄 f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } fos = new FileOutputStream(path+fileName +".xls"); fos.write(sb.toString().getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (fos!=null) { fos.close(); } } } catch (Exception e) { e.printStackTrace(); } }
下面是程式執行對照
20W條資料匯出情況下
xml方式 | jxl方式 | |
平均耗時 | 420MS左右 | 3600ms左右 |
檔案大小 | 39,800,898 位元組 | 21,436,416 位元組 |
100條資料匯出情況下
xml方式 | jxl方式 | |
平均耗時 | 6ms左右 | 300ms左右 |
檔案大小 | 20,798 位元組 | 22,528 位元組 |
這裡可以明顯看出,執行效率上,xml方式有明顯的優勢。
而資料量較小時,兩種方式的檔案大小上,並沒有多少差距,資料量大時,jxl匯出的檔案要小許多。
猜測是因為jxl匯出的excel內容壓縮過,無法使用文字方式進行檢視。
驗證方式:將jxl匯出excel轉存為xml,與xml匯出的ecxel轉存為xml檔案進行比較發現、20W資料的情況下,兩者大小沒有太大差距。