2013-04-02 189 views
5

我創造了這個代碼讀取的使用Apache POI Excel文件的內容Excel文件。我使用eclipse作爲編輯器,但是當我運行代碼時,我遇到了問題,我用粗體顯示。有什麼問題? Excel中的內容如下:閱讀使用Apache POI

Emp ID Name Salary 

1.0 john 2000000.0 

2.0 dean 4200000.0 

3.0 sam  2800000.0 

4.0 cass 600000.0 

import java.io.*; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 



public class ExcelRead { 

public static void main(String[] args) throws Exception { 
    File excel = new File ("C:\\Users\\Efi\\Documents\\test.xls"); 
    FileInputStream fis = new FileInputStream(excel); 

    HSSFWorkbook wb = new HSSFWorkbook(fis); 
    HSSFSheet ws = wb.getSheet("Input"); 

    int rowNum = ws.getLastRowNum()+1; 
    int colNum = ws.getRow(0).getLastCellNum(); 
    String[][] data = new String[rowNum][colNum]; 


    for (int i=0; i<rowNum; i++){ 
     HSSFRow row = ws.getRow(i); 
      for (int j=0; j<colNum; j++){ 
       HSSFCell cell = row.getCell(j); 
       String value = cellToString(cell); 
       data[i][j] = value; 
       System.out.println("The value is" + value); 

      } 
     } 
    } 

public static String cellToString (HSSFCell cell){ 

int type; 
Object result; 
type = cell.getCellType(); 

    switch(type) { 


    case 0://numeric value in excel 
     result = cell.getNumericCellValue(); 
     break; 
    case 1: //string value in excel 
     result = cell.getStringCellValue(); 
     break; 
    case 2: //boolean value in excel 
     result = cell.getBooleanCellValue(); 
     break; 
    default: 
     ***throw new RunTimeException("There are not support for this type of    
     cell");*** 
     } 

return result.toString(); 
} 

} 
+0

問題那朵這裏:拋出新的RuntimeException(「有不支持這種類型的細胞「); – user2235454

+0

其中是粗體行嗎?「throw new Exception ...」?,函數cellToString必須捕獲您拋出的異常或在其後面追加「拋出異常」。 – user2235520

回答

0

你應該修改的是RuntimeException的使用什麼類型不是與你的switch語句的支持信息。然後你將能夠添加對它的支持,所以不會拋出異常。

所以看你的節目做的

throw new RunTimeException("There are not support for this type of cell");

,而不是圖片,你應該添加

throw new RunTimeException("There are not support for type with id ["+type+"] of cell");

這將,告訴你什麼你錯過。如何處理這種情況取決於你。

+0

你能幫我一下我必須添加的代碼嗎? – user2235454

+0

@ user2235454,請參閱編輯。祝你好運 ! –

3

有除了你在switch語句中捕捉那些additional cell types。您有0CELL_TYPE_NUMERIC),1CELL_TYPE_STRING)和2的情況,但2CELL_TYPE_FORMULA。這裏有更多的可能值:

  • 3:CELL_TYPE_BLANK
  • 4:CELL_TYPE_BOOLEAN
  • 5:CELL_TYPE_ERROR

使用Cell常數在switch語句中的細胞類型,而不是整數文字的,並用它們全部6個來捕捉所有可能的情況。

而且@Vash已經建議,請在您的RuntimeException消息中包含實際的單元格type

2

檢查this library,我已經創建了用於讀取XLSX,XLS和CSV文件的簡單方法,使用帶有Java8的lambda表達式(使用Apache POI)。

下面是一個例子:

RowConverter<Country> converter = (row) -> new Country(row[0], row[1]); 

ExcelReader<Country> reader = ExcelReader.builder(Country.class) 
    .converter(converter) 
    .withHeader() 
    .csvDelimiter(';') 
    .sheets(1) 
    .build(); 

List<Country> list; 
list = reader.read("src/test/resources/CountryCodes.xlsx"); 
list = reader.read("src/test/resources/CountryCodes.xls"); 
list = reader.read("src/test/resources/CountryCodes.csv"); 

隨着下面的Excel和豆文件:

public static class Country { 
    public String shortCode; 
    public String name; 

    public Country(String shortCode, String name) { 
    this.shortCode = shortCode; 
    this.name = name; 
    } 
} 

Excel中:

Code Country 
ad Andorra 
ae United Arab Emirates 
af Afghanistan 
ag Antigua and Barbuda 
...