【问题标题】:Write data into new row in excel file whenever i run the code using Apache POI in Selenium WebDriver每当我在 Selenium WebDriver 中使用 Apache POI 运行代码时,将数据写入 excel 文件的新行
【发布时间】: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


【解决方案1】:

你的问题在于这三行:

    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 类(如 SheetRow)代替 XSSF 特定的类(如 XSSFSheet),就像您现在所做的那样 - 这将使您的代码同时适用于 .xlsx.xls 文件

【讨论】:

  • 感谢您的宝贵帮助,但现在我遇到了不同的错误,请您帮帮我,我已经给出了上面的修改代码。提前致谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 2016-03-13
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多