繁体   English   中英

I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError”

[英]I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError”

我想使用 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);
        }

这是错误的:

XSSFWorkbook wb = new XSSFWorkbook("fis");

应该:

XSSFWorkbook wb = new XSSFWorkbook(fis);

用文件测试这个例子

并修改 class:

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

Output:

Data from Excel is FOO

PS:我正在使用 Apache POI 4.1.2

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

暂无
暂无

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

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