簡體   English   中英

嘗試讀取Excel文件時出現非法狀態異常

[英]Illegal State exception while trying to read an excel file

我嘗試使用Apache POI庫讀取Excel文件,並收到以下錯誤:

java.lang.IllegalStateException:無法從org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue處的org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:845)的數字單元獲取文本值(XSSFCell.java:294),位於org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:246),位於com.ge.aviation.TeamDataProcessor.process(TeamDataProcessor.java:174),位於com.ge。 Aviation.BatchCall $ 1.run(BatchCall.java:30)在java.util.TimerThread.mainLoop(Timer.java:555)在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);

我的樣本來源。

            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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM