2012-01-22 17 views
5

यहाँ कोड का उपयोग कर .xls फ़ाइल उत्पन्न JXL है:प्रदर्शन JXL और POI के लिए के बीच तुलना उत्कृष्टता फ़ाइल पीढ़ियों

:

public void generateXls(String fileName, int sheets, int cols, int rows) { 

    if (cols > 256) { 
     throw new IllegalArgumentException("Error: number of cols per sheet must be < 256"); 
    } 

    if (rows > 65536) { 
     throw new IllegalArgumentException("Error: number of rows per sheet must be < 65536"); 
    } 

    String fullName = fileName + ".xls"; 
    WritableWorkbook workbook = null; 
    try { 
     workbook = Workbook.createWorkbook(new File(fullName)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Random random = new Random(); 
    for (int s = 0; s < sheets; s++) { 
     WritableSheet sheet = workbook.createSheet("Sheet" + s, 0); 
     for (int i = 0; i < cols ; i++) { 
      for (int j = 0; j < rows; j++) { 
       Number number = new Number(i, j, random.nextDouble()*1000); 
       try { 
        sheet.addCell(number); 
       } catch (RowsExceededException e) { 
        throw new RuntimeException("Error: too many rows in a sheet"); 
       } catch (WriteException e) { 
        throw new RuntimeException("Error occured while adding cell to sheet", e); 
       } 
      } 
     } 
    } 

    try { 
     workbook.write(); 
     workbook.close(); 
    } catch (WriteException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

यहाँ कोड है कि .xls और .xlsx फ़ाइलों POI का उपयोग कर उत्पन्न करता है

public void generateXlsx(String fileName, int sheets, int cols, int rows) { 

    if (cols > 16383) { 
     throw new IllegalArgumentException("Error: number of cols per sheet must be < 16383"); 
    } 
    XSSFWorkbook workbook = new XSSFWorkbook(); 

    Random random = new Random(); 
    for (int s = 0; s < sheets; s++) { 
     XSSFSheet sheet = workbook.createSheet(); 
     for (int i = 0; i < rows ; i++) { 
      XSSFRow row = sheet.createRow(i); 
      for (int j = 0; j < cols; j++) { 
       XSSFCell cell = row.createCell(j); 
       cell.setCellValue(random.nextDouble()*1000); 
      } 
     } 
    } 

    FileOutputStream fileOut = null; 
    try { 
     fileOut = new FileOutputStream(fileName); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     workbook.write(fileOut); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     fileOut.flush(); 
     fileOut.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


} 

public void generateXls(String fileName, int sheets, int cols, int rows) { 

    if (cols > 256) { 
     throw new IllegalArgumentException("Error: number of cols per sheet must be < 256"); 
    } 

    HSSFWorkbook workbook = new HSSFWorkbook(); 

    Random random = new Random(); 
    for (int s = 0; s < sheets; s++) { 
     HSSFSheet sheet = workbook.createSheet(); 
     for (int i = 0; i < rows ; i++) { 
      HSSFRow row = sheet.createRow(i); 
      for (int j = 0; j < cols; j++) { 
       HSSFCell cell = row.createCell(j); 
       cell.setCellValue(random.nextDouble()*1000); 
      } 
     } 
    } 

    FileOutputStream fileOut = null; 
    try { 
     fileOut = new FileOutputStream(fileName); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     workbook.write(fileOut); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     fileOut.flush(); 
     fileOut.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


} 

मैं एक प्रदर्शन परीक्षण लिखा है:

public static void main(String[] args) { 

    int sheets =1; 
    int cols = 255; 
    int rows = 20000; 
    long a1 = System.currentTimeMillis(); 
    ExcelFileGenerator generator = new ExcelFileGenerator(); 
    generator.generateXls("xlsJXL.xls", sheets, cols, rows); 
    long xls = System.currentTimeMillis()-a1; 
    System.out.println("xlsJXL: " + xls); 
    ExcelFileGeneratorPOI generatorPOI = new ExcelFileGeneratorPOI(); 
    long a2 = System.currentTimeMillis(); 
    generatorPOI.generateXls("xlsPOI.xls", sheets, cols, rows); 
    long xlsPoi = System.currentTimeMillis()-a2; 
    System.out.println("xlsPOI: " + xlsPoi); 
    long a3 = System.currentTimeMillis(); 
    generatorPOI.generateXlsx("xlsxPOI.xlsx", sheets, cols, rows); 
    long xlsx = System.currentTimeMillis()-a3; 
    System.out.println("xlsxPOI: " + xlsx); 
} 

परिणाम हैं: चादर रों = 1 कॉलम = 255 पंक्तियों = 10 xlsJXL: 133 xlsPOI: 162 xlsxPOI: 645

शीट = 1 कॉलम = 10 पंक्तियों = 255 xlsJXL: 130 xlsPOI: 140 xlsxPOI: 650

शीट = 10 कॉलम = 255 पंक्तियों = 255 xlsJXL: 611 xlsPOI: 784 xlsxPOI: 16228

शीट = 2 कॉलम = 100 पंक्तियों = 10000

xlsJXL: 2755 xlsPOI: 3270 xlsxPOI: सूत्र में अपवाद "मुख्य" java.lang.OutOfMemoryError: जावा ढेर अंतरिक्ष

बनाने के द्वारा किसी भी कारण से पीओआई के साथ .xlsx .xls बनाने से बहुत धीमी है?

उत्तर

5

xls एक बाइनरी आधारित प्रारूप है, xlsx एक एक्सएमएल आधारित प्रारूप है और पढ़ने/लिखने के लिए और अधिक काम की आवश्यकता है।

xlsx को XML दस्तावेज़ को पार्स/निर्माण करने के लिए स्मृति दस्तावेज़ मॉडल में भी आवश्यकता हो सकती है जो अधिक जटिल हो सकती है।

अंत में, एक्सएलएस को बेहतर अनुकूलित किया जा सकता है क्योंकि इसे लंबे समय तक समर्थित किया गया है।

+1

तो मेरे कार्यान्वयन से xlsx फ़ाइलों को तेजी से बनाने के लिए कोई समाधान नहीं है? – AAaa

+1

मुझे नहीं पता कि मैं जानता हूं। अन्य पुस्तकालय भी हो सकते हैं जो इसे तेज़ी से करते हैं। –