2017-08-30 29 views
0

私はExcelのファイル(5.9Mb、合計37列、SUM(A1:A50)のような数式を持つ7列)を読み込もうとするとメモリに問題があります。 (バージョン3.16)。ここに私のコードは次のとおりです。読んだときのメモリの問題Apache poiでファイルをExcelに変換

public static ArrayList<ReturnObject> readExcel(InputStream inputStreamExcelData) { 

    ArrayList<ReturnObject> list = new ArrayList<ReturnObject>(); 

    try { 
     //Here is the problem 
     workbook = new XSSFWorkbook(inputStreamExcelData); 
     Sheet firstSheet = workbook.getSheetAt(0); 
     Iterator<Row> iterator = firstSheet.iterator(); 

     while (iterator.hasNext()) { 
      Row nextRow = iterator.next(); 

      ReturnObject reg = new ReturnObject(); 

      Cell cell = nextRow.getCell(0); 
      reg.setData1(getValueCell(cell)); 

      cell = nextRow.getCell(1); 
      reg.setData2(getValueCell(cell)); 

      list.add(reg); 
     } 

     workbook.close(); 
     inputStreamExcelData.close(); 

    } catch (IOException e) { 
     logger.error("", e); 
    } 
    return list; 
} 

getValueCell機能:

public static String getValueCell(Cell cell) { 

    String result = ""; 

    if (cell != null) { 

     switch (cell.getCellTypeEnum()) { 
     case STRING: 
      result = cell.getStringCellValue(); 
      break; 
     case NUMERIC: 
      Double num = cell.getNumericCellValue(); 
      result = num.longValue() + ""; 
      if (DateUtil.isCellDateFormatted(cell)) { 

       String value = sdf.format(cell.getDateCellValue()); 
       return value; 
      } 
      break; 
     case FORMULA: 
      CellValue cellValue = evaluator.evaluate(cell); 
      result = cellValue.getStringValue(); 
     default: 
      break; 
     } 
    } 

    return result; 
} 

Mainメソッド

public static void main(String... args){ 
     InputStream is = new FileInputStream("/files/file.xlsm"); 
     List<ReturnObjec> r = readExcel(is); 
     System.out.ptintln(r); 
} 

クラスReturnObjec:

public class ReturnObject{ 
    private String data1; 
    private String data2; 
    ... setters and getters ... 

} 

私が初めて実行します、それは正しく動作しますが、私の記憶は私たち(58%〜74%)、ここに滞在してください。 secont attempを実行すると、メモリ使用量がもう1回77%に増加し、次にメモリリーク例外が発生します。

Exception in thread "ajp-bio-8009-AsyncTimeout" java.lang.OutOfMemoryError: GC overhead limit exceeded at java.util.concurrent.ConcurrentLinkedQueue.iterator(ConcurrentLinkedQueue.java:663) at org.apache.tomcat.util.net.JIoEndpoint$AsyncTimeout.run(JIoEndpoint.java:157)

どのようにこのメモリの問題を管理できますか?私はworkbook.close()はメモリを解放すると思ったが、それは起こらない。

+3

「リスト」の大きさはどれくらいですか? 'list'が返されるとどうなりますか? Excelに含める必要があるものとともに、[最小限の、完全で検証可能な](https://stackoverflow.com/help/mcve)の例を投稿してください。 –

+0

私は私の質問frind Andrexを更新しました。リストはリットルで、2つの要素しかありません。 –

答えて

0

XSSFとSAX(イベントAPI)を試してください。

OPCPackage pkg = OPCPackage.open(filename); 
XSSFReader r = new XSSFReader(pkg); 

詳細については、こちらを訪問XSSF SAX APISXSSF:読書は次のようになります。

+0

問題を解決できました。私はXLSX2CSVクラス(https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV)を使用しました。私の問題を解決しました。ありがとう!! –

関連する問題