gpt4 book ai didi

java - 无法写入 Excel 文件,出现 OpenXML4JRuntimeException

转载 作者:行者123 更新时间:2023-12-01 19:50:05 25 4
gpt4 key购买 nike

我正在尝试从 Excel 读取测试数据并使用测试结果更新同一张表。问题是,在第二次迭代时,Eclipse IDE 会生成异常,并且 excel 中的状态也不会更新。该文件也似乎已损坏。这是代码:

File src = new File("C:\\Users\\Sajid\\Desktop\\SeleniumData.xlsx");
FileOutputStream fos = new FileOutputStream(src, true);
FileInputStream fis = new FileInputStream(src);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);

for (int i = 1; i <= sheet.getLastRowNum(); i++)
{
// Import data for Email.
cell = sheet.getRow(i).getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"email\"]")));
driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys(cell.getStringCellValue());

String message = "Pass";
sheet.getRow(i).createCell(15).setCellValue(message);
workbook.write(fos);
}

异常(exception):

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package: The part /docProps/app.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@2e62e227

更新代码

File src = new File("C:\\Users\\Sajid\\Desktop\\SeleniumData.xlsx");
FileOutputStream fos = new FileOutputStream(src, true);
FileInputStream fis = new FileInputStream(src);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);

for (int i = 1; i <= sheet.getLastRowNum(); i++)
{
// Import data for Email.
cell = sheet.getRow(i).getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);

String message = "Pass";
sheet.getRow(i).createCell(15).setCellValue(message);
workbook.write(fos);

driver.findElement(By.cssSelector("b.hidden-xs")).click();
Thread.sleep(500);
driver.findElement(By.cssSelector("a.sign-out")).click();
Thread.sleep(1000);
}

fos.close();
fis.close();

但是我收到 cell.setCellType(Cell.CELL_TYPE_STRING); 行的空指针异常

java.lang.NullPointerException

最佳答案

如果文件不是随机访问文件,则无法同时打开文件进行输入和输出。

此外,输出不应附加到现有文件,而应替换该文件。

还要编写一次工作簿。

FileInputStream fis = new FileInputStream(src);
workbook = new XSSFWorkbook(fis);
fis.close();

FileOutputStream fos = new FileOutputStream(src); // Without append true.
...
workbook.write(fos);
fos.close();

关于java - 无法写入 Excel 文件,出现 OpenXML4JRuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com