Apache POI损坏文件

时间:2015-10-27 12:05:35

标签: java excel selenium 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");
    }
}

我很抱歉不提供网站网址。这是我们组织的内部网站。

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

控制台正在正确打印值。

由于

0 个答案:

没有答案