免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2855 | 回复: 0

java 读取excel poi 空列 [复制链接]

论坛徽章:
0
发表于 2013-06-16 13:39 |显示全部楼层
本帖最后由 itbigirl 于 2013-06-16 13:46 编辑

gdfg                 hgfh                 jhgj        jhg                                                     jhgj
以上是excl格式。
功能:读取数据,插入数据库(包括空数据)。
问题:读取不了空数据。
代码:
  1. private static List<List<Object>> read2007Excel(File file)
  2.                         throws IOException {
  3.                 List<List<Object>> list = new LinkedList<List<Object>>();
  4.                 // 构造 XSSFWorkbook 对象,strPath 传入文件路径
  5.                 XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
  6.                 // 读取第一张表格内容
  7.                 XSSFSheet sheet = xwb.getSheetAt(0);
  8.                 Object value = null;
  9.                 XSSFRow row = null;
  10.                 XSSFCell cell = null;
  11.                 for (int i = (sheet.getFirstRowNum()+1); i <=(sheet.getPhysicalNumberOfRows()-1); i++) {
  12.                         row = sheet.getRow(i);
  13.                         if (row == null) {
  14.                                 continue;
  15.                         }
  16.                         List<Object> linked = new LinkedList<Object>();
  17.                         for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
  18.                                 cell = row.getCell(j);
  19.                                 if (cell == null) {
  20.                                         continue;
  21.                                 }
  22.                                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String字符
  23.                                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化日期字符串
  24.                                 DecimalFormat nf = new DecimalFormat("0");// 格式化数字
  25.                                 switch (cell.getCellType()) {
  26.                                 case XSSFCell.CELL_TYPE_STRING:
  27.                                         System.out.println(i + "行" + j + " 列 is String type");
  28.                                         value = cell.getStringCellValue();
  29.                                         break;
  30.                                 case XSSFCell.CELL_TYPE_NUMERIC:
  31.                                         System.out.println(i + "行" + j
  32.                                                         + " 列 is Number type ; DateFormt:"
  33.                                                         + cell.getCellStyle().getDataFormatString());
  34.                                         if ("@".equals(cell.getCellStyle().getDataFormatString())) {
  35.                                                 value = df.format(cell.getNumericCellValue());
  36.                                         } else if ("General".equals(cell.getCellStyle()
  37.                                                         .getDataFormatString())) {
  38.                                                 value = nf.format(cell.getNumericCellValue());
  39.                                         } else {
  40.                                                 value = sdf.format(HSSFDateUtil.getJavaDate(cell
  41.                                                                 .getNumericCellValue()));
  42.                                         }
  43.                                         break;
  44.                                 case XSSFCell.CELL_TYPE_BOOLEAN:
  45.                                         System.out.println(i + "行" + j + " 列 is Boolean type");
  46.                                         value = cell.getBooleanCellValue();
  47.                                         break;
  48.                                 case XSSFCell.CELL_TYPE_BLANK:
  49.                                         System.out.println(i + "行" + j + " 列 is Blank type");
  50.                                         value = "";
  51.                                         break;
  52.                                 default:
  53.                                         System.out.println(i + "行" + j + " 列 is default type");
  54.                                         value = cell.toString();
  55.                                 }
  56.                                 linked.add(value);
  57.                         }
  58.                         list.add(linked);
  59.                 }
  60.                 return list;
  61.         }
复制代码
测试结果:
1行0 列 is String type
1行2 列 is String type
1行4 列 is String type
1行5 列 is String type
1行11 列 is String type
[[gdfg, hgfh, jhgj, jhg, jhgj]]
希望有人帮助解答,感谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP