2012-03-07 32 views
0
public class seventhma { 

    XSSFSheet m_sheet; 
    int m_iNbRows; 
    int m_iCurrentRow = 0; 
    private static final String JAVA_TOSTRING = "EEE MMM dd HH:mm:ss zzz yyyy"; 

    public seventhma(XSSFSheet sheet) { 
     m_sheet = sheet; 
     m_iNbRows = sheet.getPhysicalNumberOfRows(); 
    } 

    /* 
    * Returns the contents of an Excel row in the form of a String array. 
    * 
    * @see com.ibm.ccd.common.parsing.Parser#splitLine() 
    */ 
    public String[] splitLine() throws Exception { 
     // if (m_iCurrentRow == m_iNbRows) 
     // return null; 

     XSSFRow row = m_sheet.getRow(m_iCurrentRow); 
     if (row == null) { 
      return null; 
     } else { 
      int cellIndex = 0; 
      int noOfCells = row.getPhysicalNumberOfCells(); 
      String[] values = new String[noOfCells]; 
      short firstCellNum = row.getFirstCellNum(); 
      short lastCellNum = row.getLastCellNum(); 

      if (firstCellNum >= 0 && lastCellNum >= 0) { 
       for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) { 
        XSSFCell cell = (XSSFCell) row.getCell(iCurrent); 
        if (cell == null) { 
         values[iCurrent] = ""; 
         cellIndex++; 
         continue; 
        } else { 
         switch (cell.getCellType()) { 
          case XSSFCell.CELL_TYPE_NUMERIC: 
           double value = cell.getNumericCellValue(); 
           if (DateUtil.isCellDateFormatted(cell)) 

           { 
            if (DateUtil.isValidExcelDate(value)) { 
             Date date = DateUtil.getJavaDate(value); 
             SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING); 
             values[iCurrent] = dateFormat.format(date); 
            } else { 
             // throw new 
             // Exception("Invalid Date value found at row number " 
             // + 
             // row.getRowNum()+" and column number "+cell.getCellNum()); 
            } 
           } else { 
            values[iCurrent] = value + ""; 
           } 
           break; 

          case XSSFCell.CELL_TYPE_STRING: 
           values[iCurrent] = cell.getStringCellValue(); 
           break; 

          case XSSFCell.CELL_TYPE_BLANK: 
           values[iCurrent] = null; 
           break; 

          default: 
           values[iCurrent] = null; 
         } 
        } 
       } 
      } 
      m_iCurrentRow++; 
      return values; 
     } 

    } 

    public static void main(String args[]) { 
     XSSFWorkbook workBook = null; 
     File file = new File("E:\\Local\\Local2.xlsx"); 
     InputStream excelDocumentStream = null; 
     try { 
      excelDocumentStream = new FileInputStream(file); 
      // POIFSFileSystem fsPOI = new POIFSFileSystem(new 
      // BufferedInputStream(excelDocumentStream)); 
      BufferedInputStream bfs = new BufferedInputStream(excelDocumentStream); 
      workBook = new XSSFWorkbook(bfs); 
      seventhma parser = new seventhma(workBook.getSheetAt(0)); 
      String[] res = null; 
      while ((res = parser.splitLine()) != null) { 
       for (int i = 0; i < res.length; i++) { 
        System.out.println("[" + res[i] + "]" + "\t"); 

       } 
       System.out.println(res.length); 

      } 
      bfs = null; 
      excelDocumentStream.close(); 

     } catch (Exception e) { 
      System.out.println(e); 
      e.printStackTrace(); 
     } 

    } 
} 

這個節目給Java堆的空間不足和當含有列excel工作表被上傳它給ArrayIndexOutOfBoundException
我已經增加了日食記憶至-Xmx1600m,但這也沒有奏效。的OutOfMemoryError:Java堆

+1

請爲您收到的錯誤添加堆棧跟蹤。 – Turismo 2012-03-07 09:36:32

+0

堆棧跟蹤看起來如何?文件有多大? – 2012-03-07 09:37:13

+0

您是否嘗試過添加日誌記錄來確定您的程序實際上認爲該工作表有多少行和單元格?也許每次你讀一行時都有一個調試日誌記錄? – 2012-03-07 09:37:49

回答

2

由於使用row.getPhysicalNumberOfCells()來確定其大小,因此您在values陣列上得到ArrayIndexOutOfBoundException。但row.getPhysicalNumberOfCells()只會計算文件中實際填充的單元格。

例如,如果你創建一個Excel工作表,並填寫僅列A,C和F,不碰其他細胞的功能row.getPhysicalNumberOfCells()將返回3
但你通過得到row.getFirstCellNum()遍歷所有的細胞和row.getLastCellNum()。因此,values[iCurrent]肯定會在您到達單元格F後出界。

關於OutOfMemory問題: XSSF使用大量內存。嘗試將虛擬機推到儘可能多的內存中,或者,如果您只是閱讀文件,請嘗試使用eventmodel API而不是usermodel(請考慮SAX與DOM)。 Apache POI Streaming vs. In memory

相關問題