以上是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 }
View Code
测试结果:
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]]
希望有人帮助解答,感谢。