Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI将.xlsx文件中的所有单元格返回为空?_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何使用ApachePOI将.xlsx文件中的所有单元格返回为空?

Java 如何使用ApachePOI将.xlsx文件中的所有单元格返回为空?,java,excel,apache-poi,Java,Excel,Apache Poi,在谷歌搜索StackOverflow之后,我还没有找到与这个问题相关的地方。目前我可以将空白单元格读取为空,但一次只能读取一个单元格。所以我必须写这样的东西: data.setAddress(row.getCell(10, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(10).getStringCellValue()); data.setClientType(row.getCell(11, Row.RETURN_BLANK_

在谷歌搜索StackOverflow之后,我还没有找到与这个问题相关的地方。目前我可以将空白单元格读取为空,但一次只能读取一个单元格。所以我必须写这样的东西:

data.setAddress(row.getCell(10, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(10).getStringCellValue());
    data.setClientType(row.getCell(11, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(11).getNumericCellValue());
    data.setClientType0Charge(row.getCell(12, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(12).getNumericCellValue());
    data.setClientType1Charge(row.getCell(13, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(13).getNumericCellValue());
    data.setCooYears(row.getCell(14, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(14).getNumericCellValue());
    data.setConYears(row.getCell(15, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(15).getNumericCellValue());
    data.setProjectType(row.getCell(16, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(16).getNumericCellValue());
    data.setProjectTypeNotes(row.getCell(17, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(17).getStringCellValue());
    data.setChargeMethod(row.getCell(18, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(18).getNumericCellValue());
    data.setFixedPrice(row.getCell(19, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(19).getNumericCellValue());
    data.setHighTime(row.getCell(20, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(20).getStringCellValue());
    data.setHighPrice(row.getCell(21, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(21).getNumericCellValue());
    data.setPeakTime(row.getCell(22, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(22).getStringCellValue());
    data.setPeakPrice(row.getCell(23, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(23).getNumericCellValue());
    data.setPlainTime(row.getCell(24, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(24).getStringCellValue());
    data.setPlainPrice(row.getCell(25, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(25).getNumericCellValue());
    data.setValleyTime(row.getCell(26, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(26).getStringCellValue());
    data.setValleyPrice(row.getCell(27, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(27).getNumericCellValue());
    data.setServFee(row.getCell(28, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(28).getNumericCellValue());
    data.setServFeePercent(row.getCell(29, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(29).getNumericCellValue());
    data.setServFeeFixed(row.getCell(30, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(30).getNumericCellValue());
    data.setPropertyType(row.getCell(31, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(31).getNumericCellValue());
    data.setPropertyOwner(row.getCell(32, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(32).getStringCellValue());
    data.setPropertyManager(row.getCell(33, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(33).getStringCellValue());
    data.setAcquireMethod(row.getCell(34, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(34).getNumericCellValue());
    data.setAcquireMethodNotes(row.getCell(35, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(35).getStringCellValue());
    data.setSiteAddress(row.getCell(36, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(36).getStringCellValue());
    data.setSiteType(row.getCell(37, Row.RETURN_BLANK_AS_NULL) == null ? null : (int) row.getCell(37).getNumericCellValue());
    data.setSiteTypeNotes(row.getCell(38, Row.RETURN_BLANK_AS_NULL) == null ? null : row.getCell(38).getStringCellValue());

这对我来说不太好,因为我对重复感到不满。我们可以只设置
行。将整个工作表甚至整个工作簿中的单元格的\u BLANK\u返回为\u NULL
吗?

我不知道这样的全局参数,但也许您可以将逻辑封装在实用程序类中。例如,对于获取字符串值,您可以按如下操作(可能添加一些控件)

}

所以你的代码变成了

 data.setAddress(PoiUtils.getStringCellValue (row,10)); 

您可以在工作簿级别的“缺少单元格”策略中设置:

    XSSFWorkbook book = new XSSFWorkbook("h:\\excel.xlsx");
    XSSFSheet sheet = book.getSheetAt(0);
    XSSFRow row = sheet.getRow(0);
    book.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
这是因为只有单元格编号的getcell的定义如下(来自POI源代码):


正是我想要的。谢谢
    XSSFWorkbook book = new XSSFWorkbook("h:\\excel.xlsx");
    XSSFSheet sheet = book.getSheetAt(0);
    XSSFRow row = sheet.getRow(0);
    book.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
 @Override
    public XSSFCell getCell(int cellnum) {
        return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy());
    }