每次我写入excel时,它都会用空白文件覆盖整个文件 - PUZZLED

时间:2014-09-23 17:55:08

标签: java excel selenium testng jexcelapi

我想要做的是onTestFailure(TestNG)我试图将pass / fail写入指定的列。在下面的代码中,我只想做一点测试,将Fail写入指定的单元格。但是发生了什么,而不是写一个通过/失败到指定的单元格,它实际上覆盖了整个文件并删除了我的所有数据,我很困惑为什么会发生这种情况。我在这里看了google,但找不到答案。以下是以下代码:

 @Override
public synchronized void onTestFailure(ITestResult tr){
    try {
        Workbook workbook = Workbook.getWorkbook(new File(testData), workbookSettings);
        WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook);
        WritableSheet ws = wb.getSheet("OrderEditQA3");
        Label label = new Label(5,2, "Fail");
        ws.addCell(label);
        wb.write();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BiffException b){
        System.out.print("Error!");
    } catch (WriteException we){
        System.out.print("");
    }
}

非常感谢任何帮助。

更新中...

下面是另一个用于将测试数据返回到Dataprovider的类,我还从指定的工作表中加载了所有元素。以下是代码

//获取所有测试数据的代码

protected static Object[][] loadTestData(String sheetName)throws BiffException, IOException{
    Sheet sheet = null;
    try{
        sheet = getWorkBook().getSheet(sheetName);
        rowCount = sheet.getRows();
        colCount = sheet.getColumns();
        data = new String[rowCount -1][colCount-1];
        int ci;
        int cj;
        ci=0;
        for(int i=1; i< rowCount; i++, ci++){
            cj=0;
            for(int j=1; j< colCount; j++, cj++){
                Cell cell = sheet.getCell(j,i);
                if(cell.getContents().isEmpty()){ continue;}
                System.out.print(cell.getContents() + "\n");
                data[ci][cj] = cell.getContents();
            }
        }
    }catch (IOException io){
        System.out.print(String.format("File: %s not found!", testData));
    }
    getWorkBook().close();
    return data;
}

//检索所有元素属性的代码:

@BeforeClass
protected static List<String> loadElements()throws BiffException, IOException {
    Sheet sheet = null;
    sheet = getWorkBook().getSheet("Elements");
    rowCount = sheet.getRows();
    colCount = sheet.getColumns();
    List<String> list = new ArrayList<>(rowCount);
    for (int i = 1; i < rowCount; i++) {
        for (int j = 0; j < colCount; j++) {
            Cell cell = sheet.getCell(j,i);
            if(cell.getContents().isEmpty()) {continue;}
            String cellContents = cell.getContents();
            list.add(cellContents);
            System.out.print(cell.getContents() + "\n");
        }
    }
    elements = list;
    getWorkBook().close();
    return list;
}

1 个答案:

答案 0 :(得分:1)

忘记了讨厌的close()方法

@Override
public synchronized void onTestFailure(ITestResult tr){
    try {
        Workbook workbook = Workbook.getWorkbook(new File(testData), workbookSettings);
        WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook);
        WritableSheet ws = wb.getSheet("OrderEditQA3");
        Label label = new Label(5,2, "Fail");
        ws.addCell(label);
        wb.write();
        wb.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BiffException b){
        System.out.print("Error!");
    } catch (WriteException we){
        System.out.print("");
    }
}