【发布时间】:2021-12-01 21:35:45
【问题描述】:
我是 selenium 和 java 的新手,每当我在 Selenium WebDriver 中使用 Apache POI 运行代码时,我都会尝试将数据写入 excel 文件中的新行。我有以下代码。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.collect.Table.Cell;
public class ExcelWrite {
public static void main(String[] args) throws InvalidFormatException, IOException
{
File file = new File("D:\\Selenium_Training\\SeleniumFile.xlsx");
FileInputStream FIS = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = wb.getSheetAt(0);
int x = sh.getLastRowNum();
if(x==0)
{
XSSFRow row = sh.createRow(0);
row.createCell(0).setCellValue("TC001");
row.createCell(1).setCellValue("Successfully logged in");
row.createCell(2).setCellValue("Pass");
}
else
{
int y=x++;
XSSFRow row1 = sh.createRow(y);
row1.createCell(0).setCellValue("TC001");
row1.createCell(1).setCellValue("Successfully logged in");
row1.createCell(2).setCellValue("Pass");
}
FileOutputStream fout = new FileOutputStream(file);
wb.write(fout);
fout.close();
}
}
运行代码时出现以下错误,
Exception in thread "main" java.lang.IllegalArgumentException: Sheet index (0) is out of range (no sheets)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.validateSheetIndex(XSSFWorkbook.java:1061)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:848)
at day1.ExcelWrite.main(ExcelWrite.java:28)
根据给出的帮助,我修改了如下代码
public static void main(String[] args) throws InvalidFormatException, IOException
{
File file = new File("D:\\Selenium_Training\\SeleniumFile.xlsx");
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
XSSFSheet sh = wb.getSheetAt(0);
int x = sh.getLastRowNum();
if(x==0)
{
XSSFRow row = sh.createRow(0);
row.createCell(0).setCellValue("TC001");
row.createCell(1).setCellValue("Successfully logged in");
row.createCell(2).setCellValue("Pass");
}
else
{
int y=x++;
XSSFRow row1 = sh.createRow(y);
row1.createCell(0).setCellValue("TC001");
row1.createCell(1).setCellValue("Successfully logged in");
row1.createCell(2).setCellValue("Pass");
}
FileOutputStream fout = new FileOutputStream(file);
wb.write(fout);
fout.close();
}
但现在我收到以下错误,
Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:141)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:177)
at day1.ExcelWrite.main(ExcelWrite.java:57)
Caused by: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
at org.apache.poi.openxml4j.opc.PackagePart.getInputStream(PackagePart.java:500)
at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:75)
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:139)
... 2 more
您能帮忙吗,在此先感谢。
【问题讨论】:
-
你检查过你的
Excel中有多少张纸吗? -
我检查了excel文件,默认情况下它有三张表,即sheet1、sheet2和sheet3。
标签: java selenium-webdriver apache-poi