2014-02-21 97 views
1

这里我想通过使用下面的java代码来读取xl表单,但是我在阅读单元格时遇到了异常,如下所示。使用java代码读取xl sheeet

XL读码

Cell cell14 = row.getCell(14); 
int celltype = cell4.getCellType(); 
if (celltype == HSSFCell.CELL_TYPE_BOOLEAN) { 
    System.out.println("inide boolean"); 
    row.getCell(14).getBooleanCellValue(); 
    System.out.println("row.getCell(14).getBooleanCellValue()" + row.getCell(14).getBooleanCellValue()); 
} else if (celltype == HSSFCell.CELL_TYPE_ERROR) { 
    System.out.println("inide CELL_TYPE_ERROR"); 
    row.getCell(14).getErrorCellValue(); 
    System.out.println("row.getCell(14).getErrorCellValue()" + row.getCell(14).getErrorCellValue()); 
} else if (celltype == HSSFCell.CELL_TYPE_FORMULA) { 
    System.out.println("inide CELL_TYPE_FORMULA"); 
    row.getCell(14).getCellFormula(); 
    System.out.println(" row.getCell(14).getCellFormula()" + row.getCell(14).getCellFormula()); 
} else if (celltype == HSSFCell.CELL_TYPE_NUMERIC) { 
    System.out.println("inide CELL_TYPE_NUMERIC"); 
    row.getCell(14).getNumericCellValue(); 
    System.out.println("row.getCell(14).getNumericCellValue()" + row.getCell(14).getNumericCellValue()); 
} else if (celltype == HSSFCell.CELL_TYPE_STRING) { 
    System.out.println("inide CELL_TYPE_STRING"); 
    row.getCell(14).getRichStringCellValue(); 
    System.out.println("row.getCell(14).getStringCellValue();" + row.getCell(14).getRichStringCellValue()); 
} 

这里是例外

java.lang.IllegalStateException: Cannot get a text value from a numeric cell 
    at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:643) 
    at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:720) 
    at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:67) 
+0

我的电话号码有在XL是这样975125351237但里面numaric正在读这样9.788184771442E12 u能PLZ解决这个问题 – user3214269

+0

这听起来像是一个不同的问题。 xls文件中是否有类似的值? –

+0

@ user3214269格式化问题我同意这是一个不同的问题,也许参考此解决方案http://stackoverflow.com/questions/21624528/how-to-read-exact-cell-content-of-excel-file-in- apache-poi/21625090#21625090 – PopoFibo

回答

2

我认为下面错字可能是问题的原因:

Cell cell14 = row.getCell(14); 
int celltype = cell4.getCellType(); 

可能它笑ULD是:

Cell cell14 = row.getCell(14); 
int celltype = cell14.getCellType(); 

(现在的单元格类型是基于CELL14而不是小区4)

+0

我的号码在xl里面就是这样975125351237但是numaric里面是这样读的9.788184771442E12 cna u plz解决了这个问题。 – user3214269

+0

+1我打赌这是OP面临的问题 – PopoFibo

+0

我该如何解决这个问题。 – user3214269