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

我正在尝试做的是onTestFailure(TestNG)我想写一个通过/失败到指定的列。 在下面的代码中,我只是想做一些testing,将Fail写入指定的单元格。 但是现在发生的事情是,它不是写入指定单元格的合格/不合格,而是覆盖整个文件并擦除所有的数据,我为此感到困惑。 我在这里看了一下,但是找不到答案。 以下是以下代码:

@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的testing数据的另一个类,我也从指定的表中加载了所有的元素。 下面是代码

//获取所有testing数据的代码

 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; } 

只是忘了那个讨厌的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(""); } }