简体   繁体   English

如文档中所述,Apache POI MissingCellPolicy确实无法正常工作

[英]Apache POI MissingCellPolicy is not really working as described in the documentation

I'm trying to set up the Apache POI for a Data Driven automation framework in Java. 我正在尝试为Java中的数据驱动自动化框架设置Apache POI。 However, I'm kind of stuck, after several searches at the setCellData method. 但是,在对setCellData方法进行几次搜索之后,我有点卡住了。

When I try to check if a Cell in blank, the getCell throws this error: 当我尝试检查Cell是否为空时, getCell会引发以下错误:

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

And Row.RETURN_BLANK_AS_NULL is not really accepted: 并且Row.RETURN_BLANK_AS_NULL未被真正接受:

RETURN_BLANK_AS_NULL cannot be resolved or is not a field

Here's my code: 这是我的代码:

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);
            }

It does continue afterwards, but I'm interested in the 'try' part. 之后确实会继续,但是我对“尝试”部分感兴趣。

Thanks a lot, Cristian 非常感谢,克里斯蒂安

Your variable Row is a XSSFRow which does not contain the enum field RETURN_BLANK_AS_NULL . 您的变量Row是一个XSSFRow ,它不包含枚举字段RETURN_BLANK_AS_NULL

You need to import org.apache.poi.ss.usermodel.Row and then use Row.RETURN_BLANK_AS_NULL . 您需要导入org.apache.poi.ss.usermodel.Row ,然后使用Row.RETURN_BLANK_AS_NULL

I suggest you to rename your variable Row to avoid any conflict. 建议您重命名变量Row以避免任何冲突。

Add an import 添加导入

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

create a variable 创建一个变量

private static MissingCellPolicy xRow;

and apply as below 并如下申请

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

This worked for me. 这对我有用。

and if you are still find the issue with the missing policy as unexpected type, required class or package, found variable 并且如果您仍然发现缺少策略的问题是意外类型,必需的类或程序包,找到的变量

Then use this line : org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL 然后使用以下行:org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM