Apache POI破坏文件

我正在尝试使用Java中的Apache POI更新Selenium中的Excel文件。 但是,当我打开Excel文件,它显示损坏。

以下是我的Java代码。

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import javax.print.attribute.standard.JobHoldUntil; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.jboss.netty.util.internal.SystemPropertyUtil; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Test_Copy_Write1 { public static void main(String[] args) throws Exception { // get Pace Content FileInputStream paceIn = new FileInputStream(new File("D:\\News\\Prelim.xlsx")); XSSFWorkbook paceWB = new XSSFWorkbook(paceIn); XSSFSheet paceSheet = paceWB.getSheetAt(0); ByteArrayOutputStream paceOutputStream = new ByteArrayOutputStream(); XSSFWorkbook pOut = new XSSFWorkbook(); int paceRows = paceSheet.getPhysicalNumberOfRows(); System.out.println("PACE Content start"); chromeDriver.get("http://xyzwebsite.com/OpenPubSearch"); chromeDriver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td[2]/input")).sendKeys("12345"); chromeDriver.findElement(By.xpath("html/body/form/table/tbody/tr[3]/td[2]/input")).sendKeys("west"); chromeDriver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/input")).click(); for (int i = 0; i < paceRows; i++) { if (i != 0) { chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click(); Thread.sleep(1000L); } String DBName = paceSheet.getRow(i).getCell(7).toString(); getPaceNumber(chromeDriver, DBName, paceSheet, i); } // Create a output file FileOutputStream paceOut = new FileOutputStream(new File("D:\\News\\Prelim.xlsx")); pOut.write(paceOutputStream); paceOutputStream.writeTo(paceOut); paceOut.close(); chromeDriver.quit(); } private static void getPaceNumber(WebDriver chromeDriver, String dBID, XSSFSheet paceSheet, int i) throws Exception { Thread.sleep(2000L); chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea")).sendKeys(dBID); chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]")).click(); Thread.sleep(2000L); String paceNumber = (String) chromeDriver .findElement(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]")).getAttribute("value"); System.out.println("Pace number is " + paceNumber.toString() + " For DB " + dBID); XSSFCell cell = null; XSSFRow sheetRow = paceSheet.getRow(i); // Update the value of cell cell = sheetRow.getCell(1); if (cell == null) { cell = sheetRow.createCell(1); } cell.setCellValue("Pass"); } } 

在这里,我很抱歉不提供网站的url。 这是我们组织的内部网站。

请让我知道如何更新我的Excel表格而不会损坏它。

控制台正确打印值。

谢谢