itbigirl 发表于 2013-06-16 13:39

java 读取excel poi 空列

本帖最后由 itbigirl 于 2013-06-16 13:46 编辑

gdfg               hgfh               jhgj        jhg                                           jhgj
以上是excl格式。
功能:读取数据,插入数据库(包括空数据)。
问题:读取不了空数据。
代码:private static List<List<Object>> read2007Excel(File file)
                        throws IOException {
                List<List<Object>> list = new LinkedList<List<Object>>();
                // 构造 XSSFWorkbook 对象,strPath 传入文件路径
                XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
                // 读取第一张表格内容
                XSSFSheet sheet = xwb.getSheetAt(0);
                Object value = null;
                XSSFRow row = null;
                XSSFCell cell = null;
                for (int i = (sheet.getFirstRowNum()+1); i <=(sheet.getPhysicalNumberOfRows()-1); i++) {
                        row = sheet.getRow(i);
                        if (row == null) {
                                continue;
                        }
                        List<Object> linked = new LinkedList<Object>();
                        for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
                                cell = row.getCell(j);
                                if (cell == null) {
                                        continue;
                                }
                                DecimalFormat df = new DecimalFormat("0");// 格式化 number String字符
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化日期字符串
                                DecimalFormat nf = new DecimalFormat("0");// 格式化数字
                                switch (cell.getCellType()) {
                                case XSSFCell.CELL_TYPE_STRING:
                                        System.out.println(i + "行" + j + " 列 is String type");
                                        value = cell.getStringCellValue();
                                        break;
                                case XSSFCell.CELL_TYPE_NUMERIC:
                                        System.out.println(i + "行" + j
                                                        + " 列 is Number type ; DateFormt:"
                                                        + cell.getCellStyle().getDataFormatString());
                                        if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                                                value = df.format(cell.getNumericCellValue());
                                        } else if ("General".equals(cell.getCellStyle()
                                                        .getDataFormatString())) {
                                                value = nf.format(cell.getNumericCellValue());
                                        } else {
                                                value = sdf.format(HSSFDateUtil.getJavaDate(cell
                                                                .getNumericCellValue()));
                                        }
                                        break;
                                case XSSFCell.CELL_TYPE_BOOLEAN:
                                        System.out.println(i + "行" + j + " 列 is Boolean type");
                                        value = cell.getBooleanCellValue();
                                        break;
                                case XSSFCell.CELL_TYPE_BLANK:
                                        System.out.println(i + "行" + j + " 列 is Blank type");
                                        value = "";
                                        break;
                                default:
                                        System.out.println(i + "行" + j + " 列 is default type");
                                        value = cell.toString();
                                }
                                linked.add(value);
                        }
                        list.add(linked);
                }
                return list;
        }测试结果:
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
[]
希望有人帮助解答,感谢。
页: [1]
查看完整版本: java 读取excel poi 空列