2012-03-07 3 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(); 
     } 

    } 
} 

이 프로그램은 공간 때 16 열 함유 엑셀 시트 업로드가 ArrayIndexOutOfBoundException 제공 밖으로 Java 힙을 준다.
나는 이클립스의 메모리를 최대 -Xmx1600m 개까지 증가 시켰지만 그 또한 작동하지 않았다.
OutOfMemoryError가 : Java 힙

+1

발생하는 오류에 대한 스택 추적을 추가하십시오. – Turismo

+0

스택 트레이스는 어떻게 보이나요? 파일이 얼마나 큽니까? –

+0

프로그램에서 실제로 시트에 있다고 생각하는 행과 셀의 로깅을 추가 했습니까? 어쩌면 행을 읽을 때마다 디버그 로깅을 할 수 있습니까? –

답변

2

row.getPhysicalNumberOfCells()을 사용하여 크기를 확인하기 때문에 values 배열에서 ArrayIndexOutOfBoundException이 발생합니다. 그러나 row.getPhysicalNumberOfCells()은 파일에 실제로 채워지는 셀만 계산합니다.

당신은 엑셀 시트를 작성하고 열만 A, C와 F를 작성하고 터치하지 않는 경우 예를 들어 모든 row.getPhysicalNumberOfCells()에서 다른 셀은 3
돌아갑니다하지만 당신은 row.getFirstCellNum()를 얻어서 모든 셀 반복하고 row.getLastCellNum(). 그러므로 values[iCurrent]은 셀 F에 도달하면 반드시 범위를 벗어납니다.

OutOfMemory 관련 문제 : XSSF는 많은 메모리를 사용합니다. VM을 가능한 한 많은 메모리로 밀어 넣으십시오. 아니면 그냥 파일을 읽고 있다면 usermodel (SAX와 DOM을 생각해보십시오) 대신 eventmodel API를 사용해보십시오. Apache POI Streaming vs. In memory