2014-03-30 27 views
0

我嘗試讀取包含80K細胞的Android XLS文件,但它會出現以下錯誤信息:致命異常:主要 java.lang.OutOfMemoryError如何在android中加載大型xls文件?

我應該怎麼才能成功地讀取xls文件做?

由於提前, Anestis

public class ReadExcelFile 
    { 
     ArrayList<String> list = new ArrayList<String>(); 

     public ReadExcelFile() 
     { 

      Workbook workbook1 = null; 
      WorkbookSettings s = null; 
      InputStream excelContent = null; 
     try { 
      excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls"); 
      s = new WorkbookSettings(); 
      s.setUseTemporaryFileDuringWrite(true); 
      workbook1 = Workbook.getWorkbook(excelContent,s); 
      Sheet sheet = workbook1.getSheet(0);  
      ExcelMap(sheet); 
      ExcelKeyList(sheet); 

     } catch(Exception e) { 

     } finally { 

      if(workbook1 != null) { 
       workbook1.close(); 
      } 
      if(excelContent != null) { 
       try { 
       excelContent.close(); 
      } catch (IOException ex) {} 
      } 

     } 
    } 



    private void ExcelKeyList(Sheet sheet) 
    { 
     for (int i=0; i<80001; i++) 
     { 
      list.add(sheet.getCell(0,i).getContents()); 
     } 
    } 
    public ArrayList<String> getExcelKeyList() 
    { 
     return list; 
    } 

} 
+0

btw。如果OutOfMemory在ExcelKeyList中,那麼你可以使用「pager」來解決它:改變函數,這樣你可以要求1000個單元的塊,而不是一次存儲整個表 – rupps

回答

0

你超越了手機/平板電腦的能力。只是嘗試加載細胞塊,像這樣:

 public class ReadExcelFile { 

      ArrayList<String> list = new ArrayList<String>(); 
      Workbook myWorkBook; 

      // open but not close, leave it open 

      public void OpenExcelFile() 
      { 

       WorkbookSettings s = null; 
       InputStream excelContent = null; 
       try { 
        excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls"); 
        s = new WorkbookSettings(); 
        s.setUseTemporaryFileDuringWrite(true); 
        myWorkbook = Workbook.getWorkbook(excelContent,s); 

       } catch(Exception e) { 

       } 

       // we leave it open 

      } 

      // close when you are done 

      public void closeExcelFile() { 
       if(myWorkBook != null) { 
        myWorkBook.close(); 
       } 
      } 

      // Once opened, get chunks calling ExcelKeyList(0,0,1000) .... etc 

      private ArrayList<String> ExcelKeyList(int sheet,int startRow, int endRow) 
      { 
       Sheet sheet = myWorkBook.getSheet(sheet);  

       ExcelMap(sheet); 
       ExcelKeyList(sheet); 

       list.clear(); 

       for (int i=startRow; i<endRow; i++) 
       { 
        list.add(sheet.getCell(0,i).getContents()); 
       } 
       return list; 
      } 

      public ArrayList<String> getExcelKeyList() 
      { 
       return list; 
       } 

     } 

你要找出什麼是手機爆炸之前推薦的塊大小。

+0

事實上,內存不足不在excelkeylist中。 ReadExcelFile file = new ReadExcelFile();就像它讀取文件時一樣。 –

+0

然後是框架問題。檢查文檔! – rupps