繁体   English   中英

每当我使用 Selenium WebDriver 中的 Apache POI 运行代码时,将数据写入 excel 文件中的新行

[英]Write data into new row in excel file whenever i run the code using Apache POI in Selenium WebDriver

我是 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();

在这里,您犯了两个错误。 首先,您将忽略文件,并创建一个新的空工作簿。 其次, 当您有文件时 ,您尝试使用InputStream,文档解释的速度较慢并且使用了更多的内存

最好只用以下任一代码替换该代码:

    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文件

您可以使用下面的代码。但不要忘记您必须手动将excel加载到项目中。 并且该 excel文件的第一个工作表应该有一个工作表名称

public void writeDataToExcel(String email, String firstName, String lastName) throws IOException {
    try {
        FileInputStream file = new FileInputStream(new File(System.getProperty("user.dir") + "\\Data\\Deneme.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);

        Row row = sheet.createRow(sheet.getLastRowNum() + 1);
        Cell cell = row.createCell(0);
        Cell cellFn = row.createCell(1);
        Cell cellLn = row.createCell(2);
        cell.setCellValue(email);
        cellFn.setCellValue(firstName);
        cellLn.setCellValue(lastName);
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(System.getProperty("user.dir") + "\\Data\\Deneme.xlsx"));
        workbook.write(outFile);
        outFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM