简体   繁体   English

java.lang.IllegalStateException:无法从文本单元格获取数值

[英]java.lang.IllegalStateException: Cannot get a numeric value from a text cell

I have seen lot of answers over here regarding my question, but can not find solution for it. 我在这里看到了很多有关我的问题的答案,但找不到解决方案。 I am reading a excel file and storing in mysql database. 我正在读取一个Excel文件并存储在mysql数据库中。 This is my code 这是我的代码

            Sheet mySheet = wb.getSheetAt(0);
            for (Row row : mySheet) {
                int columnNumber = mySheet.getFirstRowNum();
                Cell c = row.getCell(columnNumber);
                String sql = "insert into medtest(FMCODE,FLAG,MCODE,EMPNO,NAME,ADDRESS1,ADDRESS2,ADDRESS3,BALANCE,HOSPITAL_O,HOSPITAL_I,NURSING,GENERAL,PRIVATE,SPLCODE,BKCD,ACCOUNT_NO) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement ps = (PreparedStatement) con
                        .prepareStatement(sql); 
                ps.setString(1, row.getCell(0).getStringCellValue());
                ps.setString(2, row.getCell(1).getStringCellValue());
                ps.setInt(3, (int) row.getCell(2).getNumericCellValue());
                if (c != null) {
                if (c.getCellType() == Cell.CELL_TYPE_STRING)
                    ps.setString(4, row.getCell(3).getStringCellValue());
                else if(c.getCellType() == Cell.CELL_TYPE_NUMERIC)
                    ps.setInt(4, (int) row.getCell(3).getNumericCellValue());
                }
                ps.setString(5, row.getCell(4).getStringCellValue());
                ps.setString(6, row.getCell(5).getStringCellValue());
                ps.setString(7, row.getCell(6).getStringCellValue());
                ps.setString(8, row.getCell(7).getStringCellValue());
                ps.setFloat(9, (float) row.getCell(8).getNumericCellValue());
                ps.setFloat(10, (float) row.getCell(9).getNumericCellValue());
                ps.setFloat(11, (float) row.getCell(10).getNumericCellValue());
                ps.setFloat(12, (float) row.getCell(11).getNumericCellValue());
                ps.setFloat(13, (float) row.getCell(12).getNumericCellValue());
                ps.setFloat(14, (float) row.getCell(13).getNumericCellValue());
                ps.setString(15, row.getCell(14).getStringCellValue());
                ps.setString(16, row.getCell(15).getStringCellValue());
                ps.setString(17, row.getCell(16).getStringCellValue());
                ps.executeUpdate(); 
            }

The below is my exception occuring at line ps.setString(4, row.getCell(3).getStringCellValue()); 以下是我在ps.setString(4, row.getCell(3).getStringCellValue());行发生的异常ps.setString(4, row.getCell(3).getStringCellValue());

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.getStringCellValue(HSSFCell.java:703)
    at org.stc.action.DataImport.doGet(DataImport.java:94)
    at org.stc.action.DataImport.doPost(DataImport.java:70)

The cell contains of both string and integer values 单元格包含字符串和整数值

1.Sample data : D05 (String) 1.样品数据:D05(字符串)

2.Sample data : 3916(Integer) 2.样本数据:3916(整数)

But in my table i have taken varchar as its datatype 但是在我的表中,我将varchar作为其数据类型

you have to check the type of the returned cell: 您必须检查返回的单元格的类型:

if (row.getCell(3).getCellType() == Cell.CELL_TYPE_STRING) 
   ps.setString(4, row.getCell(3).getStringCellValue());
else 
   ps.setString(4, String.valueOf(row.getCell(3).getNumericCellValue()))

Write a helper method, I had similar issue, sometimes even though you expect string (or int cell) they are of different type if they are empty: 写一个辅助方法,我遇到了类似的问题,有时即使您期望字符串(或int单元格)为空,但它们的类型也不同:

if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
 //process string value
}
else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
 //process numeric value
}
else
{
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收到此错误:消息:java.lang.IllegalStateException:无法从数字单元格获取文本值 - Getting this error: Message: java.lang.IllegalStateException: Cannot get a text value from a numeric cell 线程“主”java.lang.IllegalStateException 中的异常:无法从数字单元格中获取文本值 - Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell java.lang.IllegalStateException:无法从数字公式单元格获取文本值 - java.lang.IllegalStateException: Cannot get a text value from a numeric formula cell java.lang.IllegalStateException:将Excel工作表上传到服务器时,无法从文本单元格异常中获取数值 - java.lang.IllegalStateException: Cannot get a numeric value from a text cell Exception while uploading excel sheet to server 无法从数字单元格Java获取文本值 - Cannot get a text value from a numeric cell java java.lang.IllegalStateException:无法获得表面 - java.lang.IllegalStateException: failed to get surface java.lang.IllegalStateException - java.lang.IllegalStateException 嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为所需类型 - nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type 无法从NUMERIC单元格获取STRING值,但单元格为“文本” - Cannot get a STRING value from a NUMERIC cell, but cell is “text” Java Easymock 抱怨“java.lang.IllegalStateException:void 方法无法返回值”或“没有最后一次调用可用的模拟” - Java Easymock complains with "java.lang.IllegalStateException: void method cannot return a value" or "no last call on a mock available"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM