嗨,您可以使用JXL瓶子和一些有用的類在閱讀和使用TestNG的框架寫入Excel文件。
您可以從here
否則讀取例子Write Test Case PASS/FAIL using selenium閱讀
例子:
public class Excelutility {
public String Testcase;
public WritableSheet writablesh;
public WritableWorkbook workbookcopy;
@BeforeTest
public void queryParameterization() throws BiffException,IOException,RowsExceededException,WriteException, InterruptedException{
FileInputStream testfile = new FileInputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData.xls");
//Now get Workbook
Workbook wbook = Workbook.getWorkbook(testfile);
//Now get Workbook Sheet
Sheet sheets = wbook.getSheet("Query_data");
int Norows = sheets.getRows();
//Read rows and columns and save it in String Two dimensional array
String inputdata[][] = new String[sheets.getRows()][sheets.getColumns()];
System.out.println("Number of rows present in TestData xls file is -"+Norows);
//For writing the data into excel we will use FileoutputStream class
FileOutputStream testoutput = new FileOutputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData_results.xls");
System.out.println("creating file one");
//To Create writable workbook
workbookcopy = Workbook.createWorkbook(testoutput);
System.out.println("creating file 2");
//To Create Writable sheet in Writable workbook
writablesh = workbookcopy.createSheet("Query_data",0);
System.out.println("creating file 3");
//Using for loop to write all the data to new sheet
for(int i=0;i<sheets.getRows();i++)
{
for(int k=0;k<sheets.getColumns();k++)
{
inputdata[i][k] = sheets.getCell(k, i).getContents();
Label l = new Label(k, i, inputdata[i][k]);
Label l2 = new Label(4,0,"Results");
writablesh.addCell(l);
writablesh.addCell(l2);
}
}
}
@AfterTest
public void writeexcels(){
try {
workbookcopy.write();
} catch (IOException e) {
e.printStackTrace();
}
try {
workbookcopy.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
你得到什麼錯誤試着寫什麼時候? –
我在Testng Result view中看到這個錯誤「Java.lang.NullPointerException」 –
問題似乎是您正在嘗試讀取尚未創建的行。 – LittlePanda