用java代码读取xlsheet
在这里,我试图通过使用以下java代码来阅读xl表,但在阅读单元格时出现了异常,如下所示 Xl读取代码用java代码读取xlsheet,java,apache-poi,xls,Java,Apache Poi,Xls,在这里,我试图通过使用以下java代码来阅读xl表,但在阅读单元格时出现了异常,如下所示 Xl读取代码 Cell cell14 = row.getCell(14); int celltype = cell4.getCellType(); if (celltype == HSSFCell.CELL_TYPE_BOOLEAN) { System.out.println("inide boolean"); row.getCell(14).getBooleanCellValue();
Cell cell14 = row.getCell(14);
int celltype = cell4.getCellType();
if (celltype == HSSFCell.CELL_TYPE_BOOLEAN) {
System.out.println("inide boolean");
row.getCell(14).getBooleanCellValue();
System.out.println("row.getCell(14).getBooleanCellValue()" + row.getCell(14).getBooleanCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_ERROR) {
System.out.println("inide CELL_TYPE_ERROR");
row.getCell(14).getErrorCellValue();
System.out.println("row.getCell(14).getErrorCellValue()" + row.getCell(14).getErrorCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_FORMULA) {
System.out.println("inide CELL_TYPE_FORMULA");
row.getCell(14).getCellFormula();
System.out.println(" row.getCell(14).getCellFormula()" + row.getCell(14).getCellFormula());
} else if (celltype == HSSFCell.CELL_TYPE_NUMERIC) {
System.out.println("inide CELL_TYPE_NUMERIC");
row.getCell(14).getNumericCellValue();
System.out.println("row.getCell(14).getNumericCellValue()" + row.getCell(14).getNumericCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_STRING) {
System.out.println("inide CELL_TYPE_STRING");
row.getCell(14).getRichStringCellValue();
System.out.println("row.getCell(14).getStringCellValue();" + row.getCell(14).getRichStringCellValue());
}
这里是例外情况
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.getRichStringCellValue(HSSFCell.java:67)
我认为以下打字错误可能是问题的原因:
Cell cell14 = row.getCell(14);
int celltype = cell4.getCellType();
也许应该是:
Cell cell14 = row.getCell(14);
int celltype = cell14.getCellType();
(cellType现在是基于cell14而不是cell4。)我的号码在xl中是这样的975125351237,但在numaric内部是这样的9.788184771442E12 cna u plz解决这个问题。@user3214269你看到你的代码和建议的更改之间的区别了吗?如果不只是复制建议的更改而不是旧代码,那么只需将1放在正确的位置。975125351237是9.78818477144237E12,所以这完全符合预期。我建议你仔细阅读浮点,科学记数法,然后告诉java你想要什么格式的整数转换成字符串,我的数字在xl中是这样的975125351237,但在numaric内部是这样读的9.788184771442E12 u plz能解决这个听起来像是另一个问题的问题吗。xls文件中是否有具有类似值的行?@user3214269有关格式问题,我同意这是一个不同的问题,可能参考此解决方案,我认为这不是一个格式问题,它们的第三位数字不同。这不应该是四舍五入错误的情况。我不明白能不能请任何人解决这个问题。