加载POI的excel文件的奇怪内容

时间:2014-12-03 11:53:13

标签: java excel apache-poi

我获得了一个excel(.xls)文件,使用POI库(版本测试版本为3.9,3.10.1)似乎无法正确加载。加载后文件的内容沿着以下行:

, DIM_1, DIM_2, DIM_3, DIM_4,... ACTUAL, 0.0014, 16.0329, 240.4296,... DIM_22MAX, DIM_22MIN, , ,...

而实际内容是:

Critical Parameters & Quality Data  

Manufacturing Supplier: 
Manufacturer Supplier name: ???  
Manufacturer Supplier code: 

Sampling    Sample Size:    1
...

修改
我已经更新了该文件是使用VBA生成的。

使用的java代码:

    Workbook workbook;
    workbook = WorkbookFactory.create(fileInputStream);
    workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
    Sheet sheet = workbook.getSheetAt(0);
    FormulaEvaluator evaluator = workbook.getCreationHelper()
            .createFormulaEvaluator();

    inputMatrix = new ArrayList<>();

    for (int j = 0; j <= sheet.getLastRowNum(); j++) {
        ArrayList<Object> tRow = new ArrayList<>();
        Row row = sheet.getRow(j);
        if (row != null) {
            for (int i = 0; i < row.getLastCellNum(); i++) {
                Cell cell = row.getCell(i);
                if (cell == null)
                    tRow.add("");
                else
                    try {
                        switch (evaluator.evaluateInCell(cell)
                                .getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            String strVal = /*cell.getStringCellValue();*/ cell.getRichStringCellValue().toString(); 
                            tRow.add(strVal);
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                tRow.add(cell.getDateCellValue());
                            } else {
                                tRow.add(cell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            tRow.add(cell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            tRow.add(cell.getCellFormula());
                            break;
                        default:
                            tRow.add("");
                        }
                    } catch (Exception e) {
                        tRow.add("ERR");
                        logger.error("error evaluating excel file, assigning ERR value to the cell.", e);
                    }
            }
        }
        inputMatrix.add(tRow);
    }

有什么想法......?

1 个答案:

答案 0 :(得分:0)

事实证明,问题是第一张被隐藏的表格。