我是 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
您能帮忙吗,先谢谢了。
最佳答案
您的问题出在这三行上:
File file = new File("D:\\Selenium_Training\\SeleniumFile.xlsx");
FileInputStream FIS = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook();
这里你犯了两个错误。首先,您忽略文件并创建一个新的空工作簿。其次,你试图 use an InputStream when you have a File, which the docs explain is slower and uses more memory
您最好将该代码替换为:
File file = new File("D:\\Selenium_Training\\SeleniumFile.xlsx");
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
或者:
File file = new File("D:\\Selenium_Training\\SeleniumFile.xlsx");
Workbook wb = WorkbookFactory.open(file);
另外,使用常见的 SS 类(例如 Sheet
和 Row
)来代替 XSSF 特定的类(例如 XSSFSheet
),就像您现在所做的那样 -将使您的代码适用于 .xlsx
和 .xls
文件
关于java - 每当我在 Selenium WebDriver 中使用 Apache POI 运行代码时,将数据写入 Excel 文件的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178987/