简体   繁体   中英

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 below:

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

Tried multiple ways, but still getting the exception of main. I have created the folder as "excel" in the selenium project, in which I have pasted the 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);
        }

This is wrong:

XSSFWorkbook wb = new XSSFWorkbook("fis");

should be:

XSSFWorkbook wb = new XSSFWorkbook(fis);

Tested with file like这个例子

and modified 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: I'm using Apache POI 4.1.2

Reading an excel sheet is basically called as utils, don't mixed reading data code, and your automation code create utils class and use (property file reader)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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