【问题标题】:Apache POI MissingCellPolicy is not really working as described in the documentationApache POI MissingCellPolicy 并没有像文档中描述的那样真正工作
【发布时间】:2018-04-14 17:31:29
【问题描述】:

我正在尝试为 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);
            }

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

非常感谢, 克里斯蒂安

【问题讨论】:

    标签: java excel apache automation apache-poi


    【解决方案1】:

    添加导入

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

    创建一个变量

    private static MissingCellPolicy xRow;
    

    并申请如下

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

    这对我有用。

    【讨论】:

      【解决方案2】:

      您的变量Row 是一个XSSFRow,它不包含枚举字段RETURN_BLANK_AS_NULL。

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

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

      【讨论】:

      • 哦!命名确实有冲突。我将其更改为 xCell = xRow.getCell(ColNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);非常感谢先生!
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-01-11
        • 2022-12-12
        • 1970-01-01
        • 2018-05-31
        • 2020-04-04
        • 2017-10-31
        • 1970-01-01
        • 1970-01-01
        • 2019-07-27
        相关资源
        最近更新 更多