2013-05-22 58 views
0

IM開發一個工具,上傳Excel和驗證,但Excel文件大,行數約爲65536excel文件的讀出行明智值

這是用於上傳我的代碼和讀取Excel工作表中的值

int firstColNo = 1; 
    int rowcount = sheet.getRows(); 
    int colCount = sheet.getColumns(); 
    int row = 0; 
    String comp = ""; 
    for (row = 1; row < rowcount; row++) { 
     if (labelCell != null) { 
      cell = sheet.getCell(firstColNo, row); 
      if (cell.getContents() != null && cell.getContents().length() > 0){ 
       String compoundId = cell.getContents();    
       System.out.println(compoundId); 
      } else { 
       System.out.println("-"); 
      } 
     } 
    } 

通過閱讀它需要更多的時間來讀取行值,有沒有什麼辦法,使其更快別人需要我的代碼做任何代碼修改?

任何人都可以幫助我解決這個問題。

回答

0

這裏是你

HSSFWorkbook workbook = new HSSFWorkbook(file); 

    //Get first sheet from the workbook 
    HSSFSheet sheet = workbook.getSheetAt(0); 

    //Iterate through each rows from first sheet 
    Iterator<Row> rowIterator = sheet.iterator(); 
    while(rowIterator.hasNext()) { 
     Row row = rowIterator.next(); 

     //For each row, iterate through each columns 
     Iterator<Cell> cellIterator = row.cellIterator(); 
     while(cellIterator.hasNext()) { 

      Cell cell = cellIterator.next(); 

      switch(cell.getCellType()) { 
       case Cell.CELL_TYPE_BOOLEAN: 
        System.out.print(cell.getBooleanCellValue() + "\t\t"); 
        break; 
       case Cell.CELL_TYPE_NUMERIC: 
        System.out.print(cell.getNumericCellValue() + "\t\t"); 
        break; 
       case Cell.CELL_TYPE_STRING: 
        System.out.print(cell.getStringCellValue() + "\t\t"); 
        break; 
      } 
     } 
     System.out.println(""); 
    } 
爲例