duanlianjieQDM 2018-10-26 02:00 采纳率: 0%
浏览 1234

导入excel的时候excel中有一个单元格是空的话后边的就导入不进数据库了该在哪加判断?

private List> readExcelValue(Workbook wb) {
// 得到第一个shell
Sheet sheet = wb.getSheetAt(0);
// 得到Excel的行数
this.totalRows = sheet.getPhysicalNumberOfRows();
// 得到Excel的列数(前提是有行数)
if (totalRows > 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List> userList = new ArrayList>();
// 循环Excel行数
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
// 循环Excel的列
Map map = new HashMap();
for (int c = 0; c < this.totalCells; c++) {
Cell cell = row.getCell(c);
if (null != cell) {
if (c == 0) {
// 如果是纯数字,比如你写的是25,cell.getNumericCellValue()获得是25.0,通过截取字符串去掉.0获得25
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String name = String.valueOf(cell.getNumericCellValue());
map.put("name", name.substring(0, name.length() - 2 > 0 ? name.length() - 2 : 1));// 名称
} else {
map.put("name", cell.getStringCellValue());// 名称
}
} else if (c == 1) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String sex = String.valueOf(cell.getNumericCellValue());
map.put("sex", sex.substring(0, sex.length() - 2 > 0 ? sex.length() - 2 : 1));// 性别
} else {
map.put("sex", cell.getStringCellValue());// 性别
}
} else if (c == 2) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String age = String.valueOf(cell.getNumericCellValue());
map.put("age", age.substring(0, age.length() - 2 > 0 ? age.length() - 2 : 1));// 年龄
} else {
map.put("age", cell.getStringCellValue());// 年龄
}
} else if (c == 3) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String age = String.valueOf(cell.getNumericCellValue());
map.put("sg", age.substring(0, age.length() - 2 > 0 ? age.length() - 2 : 1));// 身高
} else {
map.put("sg", cell.getStringCellValue());// 身高
}
}

            }

        }
        // 添加到list
        userList.add(map);
    }
    return userList;
}
  • 写回答

2条回答

  • 专家-郭老师 Java领域新星创作者 2018-10-26 02:35
    关注

    应该是报错了。你自己导入前有没有判断是否为空,然后在对数据做处理。看你代码,都没有做判断

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元