急急!大侠救命!java POI 处理excel 取数据报错 。

阿鸿Leon 2012-08-27 05:10:01
excel 数据格式如下:
姓名* 学号* 省份证号* 城市 卡号 人品值
------------------------------------------------------------------------------
李爱你 43QW 12121221 1 1 好的
java代码:

sheet.getCellComment(0, i) ;
Row row = (Row) sheet.getRow(i);
StudentDto student = new StudentDto();
//获取用户信息
String name = row.getCell(0).getRichStringCellValue().toString();//姓名
String code = row.getCell(1).getRichStringCellValue().toString();//学号
String idCard = row.getCell(2).getRichStringCellValue().toString();//身份证
String instidute = row.getCell(3).getRichStringCellValue().toString();//城市
String major = row.getCell(4).getRichStringCellValue().toString();//卡号
String state = row.getCell(5).getRichStringCellValue().toString();//人品值

如上内容出现这样的情况:
当我取excle 姓名 和 学号都是OK 的 。 但是 当我取 城市 和卡号 的时候都会报错 。
我把 城市 和 卡号 改成: 1a 和 1b 的时候 又不会报错 。说到底 就是 去excle里面的整形 数据就会报错。为什么。大侠 救命 小弟分全给你。
...全文
549 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
asdf544265772 2012-08-29
  • 打赏
  • 举报
回复
最简单的方法
String cellStr=row.getCell(8).toString();
我导入excle到数据库里的思路是:
1.选择excle文件
2.上传到服务器
3.读excle文件,返回一个String[][]
4.数组当参数,传入数据库插入的方法里

如果数据库类型是int 那就在去数组值的时候 转换一下
阿鸿Leon 2012-08-29
  • 打赏
  • 举报
回复
谢谢 2 楼 这位朋友!!!虽然 你的答案不、 是我想要的 。给了我启发 。
我用下面的代码读取excel中不同类型的数据

switch(cellType)
{
case HSSFCell.CELL_TYPE_NUMERIC:
调用getNumericCellValue();break;
case HSSFCell.CELL_TYPE_STRING:
调用getStringCellValue();break;
}

我的程序里注重的是数据的格式而不是数据的类型,我希望读取到的数据的格式能够与excel中的原始格式一致,但是getNumericCellValue()返回的是Double型,那样即使excel中是整数,使用getNumericCellValue()获得的数据也被加上了小数位。我觉得POI给我画蛇添足了。所以 二楼 哥们 三克油
scbb 2012-08-27
  • 打赏
  • 举报
回复
取得cell值时要判断类型。
getCellType() ==

CELL_TYPE_BLANK
空值

CELL_TYPE_BOOLEAN
布尔型

CELL_TYPE_ERROR
错误

CELL_TYPE_FORMULA
公式型

CELL_TYPE_STRING
字符串型

CELL_TYPE_NUMERIC
数值型
brightyq 2012-08-27
  • 打赏
  • 举报
回复
取单元格内数据时,先判断一下数据类型即可。


Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println();
}
}
}
阿阿阿飞翔阿 2012-08-27
  • 打赏
  • 举报
回复
报的什么异常内容?断点一步步调试看看,
row.getCell(3).getRichStringCellValue().toString();//城市
把toString()去掉看看。
nickycheng 2012-08-27
  • 打赏
  • 举报
回复
类似这样做个判断。获取数居前,先要知道cell的类型

HSSFCell c = row.getCell(8);
if (c.getCellType() == HSSFCell.CELL_TYPE_STRING) {
String s = row.getCell(8).getRichStringCellValue().getString();
if (NumberUtils.isNumber(s)) {
return new Double(s);
} else {
return 0.0;
}
} else {
return row.getCell(8).getNumericCellValue();
}

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧