2014-04-01 76 views
0

我试着去阅读一个Excel使用Apache POI库文件,并收到以下错误:非法状态异常

java.lang.IllegalStateException: Cannot get a text value from a numeric cell at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:845) at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:294) at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:246) at com.ge.aviation.TeamDataProcessor.process(TeamDataProcessor.java:174) at com.ge.aviation.BatchCall$1.run(BatchCall.java:30) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505)

我想读在细胞中的数值作为文本值。我在Excel中的数值是9位数字(如:108010407)。

我读的Excel只是这样的:

Cell cell = row.getCell(columnNumber) 

但它读作“1.08010407E8”而不是“108010407”,我用这个值作为一个id。为了解决我的问题,我应该采取什么样的方法?

注:当我改变细胞型这样并没有什么帮助:

cell.setCellType(Cell.CELL_TYPE_STRING); 

回答

1

我的样品来源。

  switch(cell.getCellType()) { 
       case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING: 
        data = cell.getRichStringCellValue().toString(); 
        break; 
       case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC: 
        data = Integer.toString((int)cell.getNumericCellValue()); 
        break; 
      } 

的javadoc链接:http://javafind.net/library/13/poi-3.10-FINAL/docs/apidocs/org/apache/poi/xssf/usermodel/XSSFCell.html#getCellType()

+2

你会更好使用DataFormatter而非Integer.toString,前者将采用适用于该小区的的Excel格式规则 – Gagravarr