如何在Java中使用DataProvider读取Excel文件

huangapple go评论64阅读模式
英文:

How to read an Excel file in java with DataProvider

问题

我有问题阅读Excel文件中的信息,以便与Selenium一起使用进行测试。但结果总是出现NullPointerException异常。我尝试更改Excel文件中的链接。以下是这段代码的两个部分。

public class TestProviderII {

    public static void main(String[] args) {
        String url = ".\\src\\test\\resources\\dataProvider.xlsx";
        testData(url, "SheetOne");
    }

    public static void testData(String url, String sheet) {
        ExcelUtils excelUtils = new ExcelUtils(url, sheet);
        int rowCount = excelUtils.getRowCount();
        int colCount = excelUtils.getColCount();
        for (int i = 1; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                String cellData = excelUtils.getCellDataString(i, j);
                System.out.println("Data : " + cellData);
            }
        }
    }
}

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

public class ExcelUtils {

    static XSSFWorkbook workbook;
    static XSSFSheet sheet;

    public ExcelUtils(String workBook, String sheetName) {
        try {
            workbook = new XSSFWorkbook(workBook);
            sheet = workbook.getSheet(sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getRowCount() {
        int rowCount = 0;
        try {
            rowCount = sheet.getPhysicalNumberOfRows();
            System.out.println("No of Row : " + rowCount);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            e.printStackTrace();
        }
        return rowCount;
    }

    public static int getColCount() {
        int colCount = 0;
        try {
            colCount = sheet.getRow(0).getPhysicalNumberOfCells();
            System.out.println("No of Column" + colCount);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.println(e.getCause());
            e.printStackTrace();
        }
        return colCount;
    }

    public static String getCellDataString(int rowNum, int colNum) {
        String cellData = "";
        try {
            cellData = sheet.getRow(rowNum).getCell(colNum).getStringCellValue();
            System.out.println("Data Cell : " + cellData);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.println(e.getCause());
            e.printStackTrace();
        }
        return cellData;
    }

    public static Double getCellDataNumber(int rowNum, int colNum) {
        double cellData = 0;
        try {
            cellData = sheet.getRow(rowNum).getCell(colNum).getNumericCellValue();
            System.out.println(cellData);
        } catch (Exception e) {
            System.println(e.getMessage());
            System.println(e.getCause());
            e.printStackTrace();
        }
        return cellData;
    }
}

结果是NullPointerException异常

null
null
null
null
java.lang.NullPointerException
at test.ExcelUtils.getRowCount(ExcelUtils.java:23)
at test.TestProviderII.testData(TestProviderII.java:12)
at test.TestProviderII.main(TestProviderII.java:7)
java.lang.NullPointerException
at test.ExcelUtils.getColCount(ExcelUtils.java:36)
at test.TestProviderII.testData(TestProviderII.java:13)
at test.TestProviderII.main(TestProviderII.java:7)
Process finished with exit code 0

为什么尽管我按照要求做了,结果却是NullPointerException异常?我还更改了Excel文件链接,结果仍然相同。

String url = ".\\src\\test\\resources\\dataProvider.xlsx";
String url = "src\\test\\resources\\dataProvider.xlsx";
String url = "C:\\Selenuim\\demoApplication\\src\\test\\resources\\dataProvider.xlsx";
英文:

I have trouble reading the information in an Excel file to introduce and test with selenium. But the result always NullPoInterException. I tried to change the link in the Excel file. Here’s the 2-part code.

public class TestProviderII {
public static void main(String[] args) {
String url = &quot;.\\src\\test\\resources\\dataProvider.xlsx&quot;;
testData(url, &quot;SheetOne&quot;);
}
public static void testData(String url, String sheet) {
ExcelUtils excelUtils = new ExcelUtils(url, sheet);
int rowCount = excelUtils.getRowCount();
int colCount = excelUtils.getColCount();
for (int i = 1; i &lt; rowCount; i++) {
for (int j = 0; j &lt; colCount; j++) {
String cellData = excelUtils.getCellDataString(i, j);
System.out.println(&quot;Data : &quot; + cellData);
}
}
}
}
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
static XSSFWorkbook workbook;
static XSSFSheet sheet;
public ExcelUtils(String workBook, String sheetName) {
try {
workbook = new XSSFWorkbook(workBook);
sheet = workbook.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getRowCount() {
int rowCount = 0;
try {
rowCount = sheet.getPhysicalNumberOfRows();
System.out.println(&quot;No of Row : &quot; + rowCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return rowCount;
}
public static int getColCount() {
int colCount = 0;
try {
colCount = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println(&quot;No of Column&quot; + colCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return colCount;
}
public static String getCellDataString(int rowNum, int colNum) {
String cellData = &quot;&quot;;
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getStringCellValue();
System.out.println(&quot;Data Cell : &quot; + cellData);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return cellData;
}
public static Double getCellDataNumber(int rowNum, int colNum) {
double cellData = 0;
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getNumericCellValue();
System.out.println(cellData);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return cellData;
}

}

And the result is NullPointerException

null
null
null
null
java.lang.NullPointerException
at test.ExcelUtils.getRowCount(ExcelUtils.java:23)
at test.TestProviderII.testData(TestProviderII.java:12)
at test.TestProviderII.main(TestProviderII.java:7)
java.lang.NullPointerException
at test.ExcelUtils.getColCount(ExcelUtils.java:36)
at test.TestProviderII.testData(TestProviderII.java:13)
at test.TestProviderII.main(TestProviderII.java:7)
Process finished with exit code 0

Why the result nullPointerException despite I did exactly what said?
And I changed the Excel file link and the result is the same.

String url = &quot;.\\src\\test\\resources\\dataProvider.xlsx&quot;;
String url = &quot;src\\test\\resources\\dataProvider.xlsx&quot;;
String url = &quot;C:\\Selenuim\\demoApplication\\src\\test\\resources\\dataProvider.xlsx

答案1

得分: 1

将以下行更改为:

testData(url, &quot;Sheet1&quot;);

假设您的Excel文件中的表格如下所示:

如何在Java中使用DataProvider读取Excel文件

还要确保您的Excel表中有一些数据。我只需将其更改为Sheet1并运行了您的代码。如下所示:

Excel表格:

如何在Java中使用DataProvider读取Excel文件

控制台输出:

列3的数量
数据单元格:d
数据:d
数据单元格:e
数据:e
数据单元格:f
数据:f
英文:

Change below line:

testData(url, &quot;SheetOne&quot;);

to:

testData(url, &quot;Sheet1&quot;);

Assuming the sheet looks like below in your excel file:

如何在Java中使用DataProvider读取Excel文件

Also ensure, there is some data in your excel sheet. I ran your code by just changing to Sheet1. And it worked see below:

Excel sheet:

如何在Java中使用DataProvider读取Excel文件

Console Output:

No of Column3
Data Cell : d
Data : d
Data Cell : e
Data : e
Data Cell : f
Data : f

huangapple
  • 本文由 发表于 2023年2月8日 17:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定