これがばかげた質問である場合はお詫びしますが、Excelに1つのリストしか抽出できないのはなぜですか?変更してみましたが、抽出したデータと2列を並べることができない理由がよくわかりません。代わりに表示されます。
修正はありますか?
私は次のコードを持っています:
public class dads
{
public static void main(String args[]) throws Exception
{
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
SXSSFSheet sheet = workbook.createSheet("output");
FileOutputStream f = new FileOutputStream("czPA LEASE.xlsx",true);
// DRIVER
WebDriver driver=new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/List_of_Death_Note_episodes");
List<WebElement> Elements=driver.findElements(By.xpath("//table//tbody//td//i"));
int x=-1;
int y=1;
for(WebElement ele: Elements)
{
System.out.println(ele.getText());
SXSSFRow dataRow = sheet.createRow((short)++x);
SXSSFCell cell=dataRow.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(ele.getText());
}
List<WebElement> Elements1=driver.findElements(By.xpath("//table//tbody//td"));
int x1=-1;
int y1=2;
for(WebElement ele1: Elements1)
{
System.out.println(ele1.getText());
SXSSFRow dataRow = sheet.createRow((short)++x1);
SXSSFCell cell1=dataRow.createCell(1);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellValue(ele1.getText());
}
try
{
f.flush();
workbook.write(f);
f.close();
System.out.println("Excel written successfully..");
driver.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
これがあなたの答えを見つけるのに役立つことを願っています:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Test {
static WebDriver driver = new FirefoxDriver();
static WebElement element ;
public static void main(String[] args) throws InterruptedException, IOException {
String excelFileName = "C:/Test.xls";//name of excel file
String sheetName = "Sheet1";//name of sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName) ;
// DRIVER
WebDriver driver=new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/List_of_Death_Note_episodes");
List<WebElement> rows=driver.findElements(By.xpath(".//table[@class='wikitable'][1]//th[contains(@id,'ep')]"));
//iterating r number of rows
for (int r=0;r < rows.size(); r++ )
{
HSSFRow row = sheet.createRow(r);
List<WebElement> cloumns = driver.findElements(By.xpath(".//table[@class='wikitable'][1]//th[@id='ep"+r+"']//parent::tr[1]/td"));
//iterating c number of columns
for (int c=0;c < cloumns.size(); c++ )
{
HSSFCell cell = row.createCell(c);
cell.setCellValue(cloumns.get(c).getText());
}
}
FileOutputStream fileOut = new FileOutputStream(excelFileName);
//write this workbook to an Outputstream.
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加