使用POI在Excel中写入数据。它删除了我的整个工作簿?

时间:2017-03-29 06:18:17

标签: java apache-poi

我是Selenium的新人。我试图通过获取activeRow并在列标题中搜索,在Excel文件中写入数据。

我在驱动程序脚本类中的代码如下:

int activeRow = cnfig.isScenarioAvailable("New", cn);
    if (activeRow != 0) {
        String[] colHeading = new String[cnfig.getcolumnCount("New")];
        colHeading = cnfig.getcolumnHeadinginArray("New");

for (int counter = 0; counter < cnfig.getcolumnCount("New"); counter++) {
    if (colHeading[counter].equals("Name")) {
        inName = cn + " Test" + cmnfnc.getRandomNumber(5000, 100);
        cnfig.setData(activeRow, counter, "Name", inName);
    }    

我的配置类中的代码是

 XSSFWorkbook wb;
 XSSFSheet sht;

 File fl;
 FileInputStream fis;
 FileOutputStream fout;

 public ExcelConfig(String filePath) {
    try {
        fl = new File(filePath);
        fis = new FileInputStream(fl);
        wb = new XSSFWorkbook(fis);
        fout = new FileOutputStream(fl);

    } 

public void setData(int activeRow, int activeCol, String sheetName, String value) {
    try {
        sht = wb.getSheet(sheetName);
        sht.getRow(activeRow).createCell(activeCol).setCellValue(value);
        wb.write(fout);
    }

1 个答案:

答案 0 :(得分:0)

这不是硒问题,请删除硒标签。

如果要使用poi编辑excel工作簿,则必须首先加载文件输入流

public void setData(int activeRow, int activeCol, String sheetName, String value) {

    try {
        FileInputStream in = new FileInputStream(yourFileLocation);
        XSSFWorkbook wb    = new XSSFWorkbook(in);  //I know this line is not suppose to be here, but I hope you get the general idea
        sht                = wb.getSheet(sheetName);

        sht.getRow(activeRow).createCell(activeCol).setCellValue(value);
        wb.write(fout);
    }
}