Excel数值单元格导入数据库问题:需兼容INT类型,修改代码无效
解决Excel导入数据库时INT类型数据的问题
我看到你在尝试将Excel数据导入数据库时,遇到了INT类型字段无法正确导入的问题,咱们先揪出代码里的核心问题:
你当前代码的错误点
你写的这两行是关键问题所在:
pst.setInt(2, row.getCell(1).getCellType()); pst.setInt(4, row.getCell(3).getCellType());
getCellType()方法返回的是单元格的类型枚举值(比如数值型对应CellType.NUMERIC的整数值),而不是单元格里实际存储的数字内容——这就导致你插入数据库的不是Excel里的真实数据,而是类型标识,肯定不符合你的需求。
解决方案:安全获取单元格的整数值
我们需要一个能兼容不同单元格类型(数值型、文本型数字)、同时处理空单元格的转换方法,确保能正确拿到INT型数据。
第一步:添加工具方法
在你的控制器类里加入这个工具方法,统一处理单元格到int的转换逻辑:
private int getCellIntValue(Row row, int cellIndex) { Cell cell = row.getCell(cellIndex); // 处理空单元格,这里返回0作为默认值,你可以根据业务需求调整(比如允许null的话要改逻辑) if (cell == null) { return 0; } switch (cell.getCellType()) { case NUMERIC: // 如果是数值型,直接转成int;如果需要四舍五入,把这里改成Math.round后再转 return (int) cell.getNumericCellValue(); case STRING: // 如果是文本格式的数字,先去除空格再转int String cellValue = cell.getStringCellValue().trim(); try { return Integer.parseInt(cellValue); } catch (NumberFormatException e) { // 抛出异常方便定位哪一行哪一列出了问题,也可以返回默认值 throw new RuntimeException("第" + (row.getRowNum() + 1) + "行,第" + (cellIndex + 1) + "列的内容不是有效的整数: " + cellValue, e); } default: throw new IllegalArgumentException("第" + (row.getRowNum() + 1) + "行,第" + (cellIndex + 1) + "列的类型不支持转换为整数,类型是: " + cell.getCellType()); } }
第二步:修改导入代码
把原来的错误代码替换成调用这个工具方法,同时增加空行判断避免空指针:
@FXML private void importExcel(){ try { conn = Database.connectdb(); String query = "Insert into registratie(naam, lostandfoundID, kenmerken, labelnummer, luchthaven) values (?,?,?,?,?)"; pst = conn.prepareStatement(query); String excelFilePath = "Bagage.xlsx"; try (FileInputStream fileIn = new FileInputStream(new File(excelFilePath)); XSSFWorkbook wb = new XSSFWorkbook(fileIn)) { XSSFSheet sheet = wb.getSheetAt(0); Row row; for(int i=1; i<=sheet.getLastRowNum(); i++){ row = sheet.getRow(i); if (row == null) continue; // 跳过空行,避免空指针 pst.setString(1, row.getCell(0).getStringCellValue()); // 替换为工具方法获取真实的int值 pst.setInt(2, getCellIntValue(row, 1)); pst.setString(3, row.getCell(2).getStringCellValue()); // 同样替换这里 pst.setInt(4, getCellIntValue(row, 3)); pst.setString(5, row.getCell(4).getStringCellValue()); pst.execute(); } Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("BagageOverzicht"); alert.setHeaderText(null); alert.setContentText("Data succesvol geïmporteerd!"); alert.showAndWait(); } pst.close(); rs.close(); } catch (SQLException | FileNotFoundException ex) { Logger.getLogger(BagageOverzichtController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(BagageOverzichtController.class.getName()).log(Level.SEVERE, null, ex); } catch (RuntimeException | IllegalArgumentException ex) { // 捕获自定义异常,给用户明确的错误提示 Alert alert = new Alert(AlertType.ERROR); alert.setTitle("导入错误"); alert.setHeaderText(null); alert.setContentText(ex.getMessage()); alert.showAndWait(); Logger.getLogger(BagageOverzichtController.class.getName()).log(Level.SEVERE, null, ex); } }
额外注意事项
- 确保数据库中的
lostandfoundID和labelnummer字段确实是INT类型,和插入的数据类型匹配; - 如果Excel里的数值带小数(比如
123.5),默认代码会直接截断为123,需要四舍五入的话,把return (int) cell.getNumericCellValue();改成return (int) Math.round(cell.getNumericCellValue());; - 空单元格的处理逻辑可以根据业务调整,比如数据库字段允许为空的话,要把返回值改成
Integer并处理null,同时使用pst.setObject方法插入。
内容的提问来源于stack exchange,提问作者Ans Loecm
相关产品推荐
相关产品推荐

