我尝试使用 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/