Apache POI MissingCellPolicy实际上并没有像文档中所描述的那样工作

时间:2017-11-02 08:56:00

标签: java excel apache automation apache-poi

我正在尝试为Java中的数据驱动自动化框架设置Apache POI。但是,在使用setCellData方法进行多次搜索后,我有点卡住了。

当我尝试检查单元格是否为空时,getCell会抛出此错误:

The method getCell(int, Row.MissingCellPolicy) in the type XSSFRow is
not applicable for the arguments (int, Class<Row>)

Row.RETURN_BLANK_AS_NULL并未真正被接受:

RETURN_BLANK_AS_NULL cannot be resolved or is not a field

这是我的代码:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;

    public static void setCellData(String Result, int RowNum, int ColNum) throws Exception {

        try {
            Row = ExcelWSheet.getRow(RowNum);
            Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
            if (Cell == null) {
                Cell = Row.createCell(ColNum);
                Cell.setCellValue(Result);
            } else {
                Cell.setCellValue(Result);
            }

之后会继续,但我对“尝试”感兴趣。一部分。

非常感谢, 克里斯蒂安

3 个答案:

答案 0 :(得分:2)

添加导入

import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;

创建变量

private static MissingCellPolicy xRow;

并按以下方式申请

Cell = Row.getCell(ColNum, xRow.RETURN_BLANK_AS_NULL);

这对我有用。

答案 1 :(得分:1)

您的变量RowXSSFRow,不包含枚举字段RETURN_BLANK_AS_NULL

您需要导入org.apache.poi.ss.usermodel.Row,然后使用Row.RETURN_BLANK_AS_NULL

我建议您重命名变量Row以避免任何冲突。

答案 2 :(得分:0)

如果您仍然发现缺少政策的问题  作为意外类型, 所需的课程或包裹,  找到变量

然后使用这一行: org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL

相关问题