我想用java从selenium中的excel中读取数据,但它将异常作为“main”java.lang.ExceptionInInitializeError抛出

cczfrluj  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(386)

我想使用java从selenium中的excel中读取数据,但它引发了如下异常:

"main" java.lang.ExceptionInInitializerErrorat org.apache.poi.ss.util.CellReference.<init>(CellReference.java:110).

尝试了多种方法,但是仍然得到了main的例外。我在selenium项目中创建了“excel”文件夹,并在其中粘贴了excel。

package utils;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;

    public class GetRowCount {

        public static void main(String[] args) throws Exception {
    ReadExcel();
    }

        public static void ReadExcel() 
    {

            File src = new File("C:....");
            FileInputStream fis = new FileInputStream(src);

            XSSFWorkbook wb = new XSSFWorkbook("fis");

            XSSFSheet sheet1 = wb.getSheetAt(0);

            String data0 =sheet1.getRow(0).getCell(0).getStringCellValue();

            System.out.println("Data from Excel is "+data0);
        }
jckbn6z7

jckbn6z71#

读取excel工作表基本上称为utils,不要混合读取数据代码,您的自动化代码创建utils类并使用(属性文件读取器)

odopli94

odopli942#

这是错误的:

XSSFWorkbook wb = new XSSFWorkbook("fis");

应该是:

XSSFWorkbook wb = new XSSFWorkbook(fis);

用锉刀测试

和修改类:

package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class GetRowCount {

    public static void main(String[] args) throws Exception {
        ReadExcel();
    }

    public static void ReadExcel() throws IOException {
        File src = new File("C:\\test.xlsx");
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1 = wb.getSheetAt(0);
        String data0 = sheet1.getRow(0).getCell(0).getStringCellValue();
        System.out.println("Data from Excel is " + data0);
        // don't forget to close the workbook
        wb.close();
    }
}

输出:

Data from Excel is FOO

ps:我正在使用ApachePOI4.1.2

相关问题