我想在excel中编写webtable值。我尝试了它与列表,但它没有以正确的格式写。它只写行中的所有值。
ArrayList<String> Storetablevalues = new ArrayList<String>();
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("URL");
}
@Test
public void run() throws Exception {
/* driver.findElement(By.xpath(".//*[@id='results']/div/a")).click(); */
Thread.sleep(9000);
System.out.println("Values are loaded");
int Row_count = driver.findElements(By.xpath("//*[@id='assetsTable']/tbody/tr")).size();
System.out.println("Number Of Rows = " + Row_count);
// Get number of columns In table.
int Col_count = driver.findElements(By.xpath("//*[@id='assetsTable']/tbody/tr[2]/td")).size();
System.out.println("Number Of Columns = " + Col_count);
// divided xpath In three parts to pass Row_count and Col_count values.
String first_part = "//*[@id='assetsTable']/tbody/tr[";
String second_part = "]/td[";
String third_part = "]";
String[][] arr = new String[Row_count][Col_count]; // Used for loop for
// number of rows.
for (int i = 2; i <= Row_count; i++) {
// Used for loop for number of columns.
for (int j = 1; j <= Col_count; j++) {
// Prepared final xpath of specific cell as per values of i and
// j.
String final_xpath = first_part + i + second_part + j + third_part;
// Will retrieve value from located cell and print It.
String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
Storetablevalues.add(Table_data);
System.out.print(Table_data + " ");
}
System.out.println("");
System.out.println("");
}
}
public void WriteXL() throws Exception {
// Write a xl
try {
File exlFile = new File("C:/Users/Kishor/Desktop/gtmetrix.xls");
WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile);
WritableSheet writableSheet = writableWorkbook.createSheet("Sheet2", 0);
Label Header_Url_label = new Label(0, 0, "URL");
Label Header_Image_label = new Label(1, 0, "Link Check Status");
writableSheet.addCell(Header_Url_label);
writableSheet.addCell(Header_Image_label);
for (int i = 0; i < Storetablevalues.size(); i++) {
int j = i + 1;
Label labelURL = new Label(0, j, Storetablevalues.get(i));
writableSheet.addCell(labelURL);
}
// Write and close the workbook
writableWorkbook.write();
writableWorkbook.close();
System.out.println("Xls Writer...");
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
} This is my console Output I want to write in this format in excle
答案 0 :(得分:0)
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class TableToExcel {
String[][] tableVal;
int rowCount,columnCount;
WebDriver driver = new FirefoxDriver();
private static final String FILE_NAME = "C:/MyFirstExcel.xls";
//https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro
@BeforeClass
public void getTableSize(){
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro");
driver.switchTo().frame("iframeResult");
//get Row size
List<WebElement> row = driver.findElements(By.xpath(".//table/tbody/tr"));
//get Column size
List<WebElement> column = driver.findElements(By.xpath(".//table/tbody/tr/th"));
rowCount = row.size();
columnCount = column.size();
System.out.println("Row :"+rowCount+" Clounm :"+columnCount);
tableVal = new String[rowCount][columnCount];
}
@Test
public void test() throws IOException{
for(int i =1 ; i <= rowCount ; i++ ){
for(int j =1 ; j <= columnCount ; j++ ) {
if(i == 1) {
//Get header value
tableVal[i - 1][j - 1] = driver.findElement(By.xpath(".//table/tbody/tr[" + i + "]/th[" + j + "]")).getText();
System.out.println(driver.findElement(By.xpath(".//table/tbody/tr[" + i + "]/th[" + j + "]")).getText());
}
else{
//get table data values
tableVal[i-1][j-1] =driver.findElement(By.xpath(".//table/tbody/tr["+i+"]/td["+j+"]")).getText();
System.out.println(driver.findElement(By.xpath(".//table/tbody/tr["+i+"]/td["+j+"]")).getText());
}
}
}
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Datatypes in Java");
int rowNum = 0;
System.out.println("Creating excel");
for (Object[] datatype : tableVal) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field : datatype) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
workbook.close();
}
System.out.println("Done");
}
}