2014-09-18 49 views
1

我使用POI 3.9和SXSSF來創建電子表格。當有少量的行時,創建超鏈接就可以工作。但是,隨着規模的增長,處理時間大大增加,鏈接不起作用。它適用於700行,但不是70,000。當試圖打開該文件時,會發生此錯誤:爲什麼使用POI編寫大型電子表格時超鏈接停止工作?

Excel在'out.xlsx'中發現無法讀取的內容。你想恢復這個工作簿的內容嗎?如果您信任此工作簿的來源,請單擊是。

public static void main(String[] args) throws Throwable { 
    Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk 
    Sheet sh = wb.createSheet(); 
    for(int rownum = 0; rownum < 70000; rownum++){ 
     Row row = sh.createRow(rownum); 
     for(int cellnum = 0; cellnum < 10; cellnum++){ 
      Cell cell = row.createCell(cellnum); 
      String address = new CellReference(cell).formatAsString(); 
      cell.setCellValue(address); 
      if (address.contains("A")) 
       setHyperlink(cell, address); 
     } 
    } 

    FileOutputStream out = new FileOutputStream("C:/temp/sxssf.xlsx"); 
    wb.write(out); 
    out.close(); 
} 

protected static void setHyperlink(Cell cell, String address) { 
    Workbook workbook = cell.getSheet().getWorkbook(); 
    Hyperlink hyperlink = workbook.getCreationHelper() 
      .createHyperlink(Hyperlink.LINK_URL); 
    String encoded = null; 
    try { 
     encoded = URLEncoder.encode(address, "UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    hyperlink.setAddress(encoded); 
    cell.setHyperlink(hyperlink); 
} 

回答

相關問題