有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当单元格包含空值时从excel获取数据的java

我正在从excel中获取数据,我需要计算某些单元格包含空值的行数。代码没有返回任何内容。对于此单元格中包含某些值的行,它工作正常。你能帮我处理空值单元格吗

代码如下:

HSSFSheet sheet = workbook.getSheetAt(1);

        // Get iterator to all the rows in current sheet
        Iterator<Row> rowIterator = sheet.iterator();

        // Iterate through rows
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            // Index of column D is 3 (A->0, B->1, etc)
            Cell cellD = row.getCell(3);
            Cell cellG = row.getCell(6);

            //
            String key = null;
            Integer value = null;

            // finding cell type
            int cellTypeG = cellG.getCellType();

            // getting value from first cell
            key = cellD.getStringCellValue();


            // fetching value to the Double, to be able to compare data
            if (Cell.CELL_TYPE_NUMERIC == cellTypeG) {
                value = new Double(cellG.getNumericCellValue()).intValue();
                 if (value != null) {
                    if (map.containsKey(key)) {
                        map.put(key, map.get(key) + 1);
                    } else {
                        map.put(key, 1);
                    }

                } 
            }
        }

        // cycle which is filling the map
        for (Entry<String, Integer> e : map.entrySet()) {
            System.out.println(e.getKey() + ": " + e.getValue());
        }

共 (1) 个答案

  1. # 1 楼答案

    Can you help me how to handle cells with null value?

    只需使用StringUtils.isEmpty()检查单元格是否为空/黑

    例如,StringUtils.isEmpty(cellD.getStringCellValue())。如果单元格为空或null,则返回true