簡體   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