我尝试将包含7个名为Excel.xls表的colomns的Excel工作表复制到ExcelCopy.xls中,但在@After Test中获取Java Null Exception错误并且对于这个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 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();
}
}
答案 0 :(得分:1)
我假设您要复制完整的Excel工作簿。如果,那么请使用FileUtil
,这样更容易编码。好的是你不需要担心目的地目录,文件等。如果目的地存在,它也会被覆盖。
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();
}
确保您拥有以下导入
import org.apache.commons.io.FileUtils;
Maven repo here
答案 1 :(得分:0)
看起来您将fo
声明为类成员,但您没有在任何地方分配它。因此,当您尝试调用{{1}}
如果未使用{{1}},我建议您将其从此课程中删除。