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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题