org.apache.poi.xssf.usermodel.XSSFCell 中 java 中的 Java Excel 数据上传错误

标签 java apache apache-poi

我尝试使用 Apache.POI 将 excel 数据上传到数据库中。他们的功能给我一个错误,如

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFCell cannot be cast to java.lang.Integer

代码段错误发生在

for (Iterator iterator = dataHolder.iterator(); iterator.hasNext();) {
        List list = (List) iterator.next();
         i++;
        if (i > 0) {  
            ID =  (int) list.get(0);
            Employee_Number = list.get(1).toString();
            FirstName = list.get(2).toString();
            LastName = list.get(3).toString();
            EmailAddress = list.get(4).toString();
            PdfName = list.get(5).toString();
            Sup_EmailAddress = list.get(6).toString();
            PassCode = list.get(7).toString();

谁能告诉我为什么会出现这种错误?

最佳答案

根据official JavaDoc of XSSFCell您可以改用 getNumericCellValue()

public double getNumericCellValue()

Get the value of the cell as a number.

为了安全地检查您是否处理了实际数字,您可以按如下方式重写您的代码片段:

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;

int ID;
// ... loop here ...
    XSSFCell cell = list.get(0);
    if(CellType.NUMERIC == cell.getCellType()) {
        ID =  new Double(cell.getNumericCellValue()).intValue(); 
    } else {
        // handle this case separately... 
    }
// end loop

上面的代码片段避免了不必要的转换和/或尝试和错误猜测。

希望对您有所帮助。

关于org.apache.poi.xssf.usermodel.XSSFCell 中 java 中的 Java Excel 数据上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52113977/

相关文章:

java - 为什么可变参数总是方法签名中的最后一个参数?

java - hibernate/ Spring 回滚插入没有错误

apache - 在转发代理后面设置 apache

apache - 拒绝服务 - 如何防止这种情况

java - 如何使用apache poi从.docx文档中获取图片和表格?

java - 我的错误在哪里?

java - 没有背景单元格的 HSSFColor 返回错误的背景颜色

java - 延迟分组网络请求

apache - mod_rewrite 和相对 url

java - 相互 SSL 异常发送警报 : java.net.SocketException:管道损坏(写入失败)