Selenium Webdriver复制ExcelSheet [英] Selenium Webdriver Duplicating ExcelSheet

查看:131
本文介绍了Selenium Webdriver复制ExcelSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个包含名为Excel.xls表的7个colomns的Excel表复制到ExcelCopy.xls中,但是在@After Test中获取Java Null异常错误,并且对于这个selenium编码来说是非常新的,请帮助我!

  package TestNG; 


import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class DuplicateExcelSheet {
WebDriver驱动程序;
WebDriverWait等待;

工作簿w;
Sheet s;
FileInputStream fi;
FileOutputStream fo;
WritableWorkbook ww;
WritableSheet ws;
@Test
public void f()throws Exception {
int colCount = s.getColumns();
System.out.println(colCount);
ww = Workbook.createWorkbook(fo,w);
ws = ww.createSheet(Data,0); (int i = 0; i
String s1 = s.getCell(i,0).getContents();
Label l = new Label(i,0,s1);
ws.addCell(l);
}
}

@BeforeTest
public void beforeTest()throws Exception {

fi = new FileInputStream(E:\ \selenium\\Excel.xls);
w = Workbook.getWorkbook(fi);
s = w.getSheet(0);
fo = new FileOutputStream(E:\\\selenium\\\ExcelCopy.xls);
}

@AfterTest
public void afterTest()throws异常{
ww.write();
w.close();
fi.close();
fo.close();


}

}


解决方案

我假设你要复制完整的excel工作簿。如果,所以使用 FileUtil 这是更容易和更少的编码。好的是,您不需要担心目的地目录,文件等。如果目的地存在,它也将被覆盖。

 文件sourceExcel =新文件(D:\\Users\\Saifur\\\Desktop\\Delete\\\excelFrom\\Selenium.xlsx); 
文件dstExcel =新文件(D:\\\\\Saifur\\\Desktop\\Delete\\excelTo\\\Selenium_Copy.xlsx);

try {

FileUtils.copyFile(sourceExcel,dstExcel);

} catch(IOException e){

e.printStackTrace();
}

确保您具有以下导入

  import org.apache.commons.io.FileUtils; 

Maven repo here


I tried to copy an excel sheet containing 7 colomns named Excel.xls sheet into ExcelCopy.xls , but Getting an Java Null Exception error at @After Test and as am very new to this selenium coding please help me !!

 package TestNG;


import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class DuplicateExcelSheet {
    WebDriver driver;
    WebDriverWait wait;

    Workbook w;
    Sheet s;
    FileInputStream fi;
    FileOutputStream fo;
    WritableWorkbook ww;
    WritableSheet ws;
  @Test
  public void f() throws Exception{
    int colCount=s.getColumns();
    System.out.println(colCount);
    ww=Workbook.createWorkbook(fo, w);
    ws=ww.createSheet("Data", 0);

    for (int i = 0; i < colCount; i++) 
    {
    String s1=s.getCell(i, 0).getContents();
     Label l=new Label(i,0,s1);
     ws.addCell(l);
    }
     }

  @BeforeTest
  public void beforeTest() throws Exception{

      fi=new FileInputStream("E:\\selenium\\Excel.xls");
        w=Workbook.getWorkbook(fi);
        s=w.getSheet(0);
          fo=new FileOutputStream("E:\\selenium\\ExcelCopy.xls");
      }

  @AfterTest
  public void afterTest() throws Exception{
       ww.write();
       w.close();
      fi.close();
      fo.close();


  }

}

解决方案

I assume you want to copy the full excel workbook. If, so use FileUtil which is much more easier and lot less coding. Good thing is you do not need to worry about the destination directory, files etc. It will also overwrite if the destination exists.

File sourceExcel = new File("D:\\Users\\Saifur\\Desktop\\Delete\\excelFrom\\Selenium.xlsx");
File dstExcel = new File("D:\\Users\\Saifur\\Desktop\\Delete\\excelTo\\Selenium_Copy.xlsx");

try {

    FileUtils.copyFile(sourceExcel, dstExcel);

} catch (IOException e) {

    e.printStackTrace();
}

Make sure you have the following import

import org.apache.commons.io.FileUtils;

Maven repo here

这篇关于Selenium Webdriver复制ExcelSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆