如何覆盖我以前的Excel文件使用JXL?

我想设置执行testing的状态,但是每当执行代码时,都会使用这一行创build新的工作簿:

wwbCopy = Workbook.createWorkbook(new File(testSuitePath)); 

现在任何人都可以让我知道如何设置执行testing用例的状态。

为此,我正在使用JXL lib。 并在keydriven框架。

下面是在Excel中写入的代码:

  public class MainClass { private static final String BROWSER_PATH = "D:\\softs\\FF installed\\FF18\\firefox.exe"; private static final String TEST_SUITE_PATH = "D:\\GmailTestSuite.xls"; private static final String OBJECT_REPOSITORY_PATH = "D:\\objectrepository.xls"; private static final String ADDRESS_TO_TEST = "https://www.gmail.com"; private WebDriver driver; private Properties properties; public MainClass() { File file = new File(BROWSER_PATH); FirefoxBinary fb = new FirefoxBinary(file); driver = new FirefoxDriver(fb, new FirefoxProfile()); driver.get(ADDRESS_TO_TEST); } public static void main(String[] args) throws Exception { MainClass main = new MainClass(); main.handleTestSuite(); } private void handleTestSuite() throws Exception { ReadPropertyFile readConfigFile = new ReadPropertyFile(); properties = readConfigFile.loadPropertiess(); ExcelHandler testSuite = new ExcelHandler(TEST_SUITE_PATH, "Suite"); testSuite.columnData(); int rowCount = testSuite.rowCount(); System.out.println("Total Rows=" + rowCount); for (int i = 1; i < rowCount; i++) { String executable = testSuite.readCell( testSuite.getCell("Executable"), i); System.out.println("Executable=" + executable); if (executable.equalsIgnoreCase("y")) { // exe. the process String scenarioName = testSuite.readCell( testSuite.getCell("TestScenario"), i); System.out.println("Scenario Name=" + scenarioName); handleScenario(scenarioName); } } WritableData writableData= new WritableData(TEST_SUITE_PATH,"Login"); writableData.shSheet("Login", 5, 1, "Pass"); writableData.shSheet("Login", 5, 2, "Fail"); writableData.shSheet("Login", 5, 3, "N/A"); } private void handleScenario(String scenarioName) throws Exception { ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH); testScenarios.setSheetName("Login"); testScenarios.columnData(); int rowWorkBook1 = testScenarios.rowCount(); for (int j = 1; j < rowWorkBook1; j++) { String framWork = testScenarios.readCell( testScenarios.getCell("FrameworkName"), j); String operation = testScenarios.readCell( testScenarios.getCell("Operation"), j); // SendKey String value = testScenarios.readCell( testScenarios.getCell("Value"), j); System.out.println("FRMNameKK=" + framWork + ",Operation=" + operation + ",Value=" + value); handleObjects(operation, value, framWork); } } private void handleObjects(String operation, String value, String framWork) throws Exception { System.out.println("HandleObject--> " + framWork); ExcelHandler objectRepository = new ExcelHandler( OBJECT_REPOSITORY_PATH, "OR"); objectRepository.columnData(); int rowCount = objectRepository.rowCount(); System.out.println("Total Rows in hadleObject=" + rowCount); for (int k = 1; k < rowCount; k++) { String frameWorkName = objectRepository.readCell( objectRepository.getCell("FrameworkName"), k); String ObjectName = objectRepository.readCell( objectRepository.getCell("ObjectName"), k); String Locator = objectRepository.readCell( objectRepository.getCell("Locator"), k); // SendKey System.out.println("FrameWorkNameV=" + frameWorkName + ",ObjectName=" + ObjectName + ",Locator=" + Locator); if (framWork.equalsIgnoreCase(frameWorkName)) { operateWebDriver(operation, Locator, value, ObjectName); } } } private void operateWebDriver(String operation, String Locator, String value, String objectName) throws Exception { System.out.println("Operation execution in progress"); WebElement temp = getElement(Locator, objectName); if (operation.equalsIgnoreCase("SendKey")) { temp.sendKeys(value); } Thread.sleep(1000); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); if (operation.equalsIgnoreCase("Click")) { temp.click(); } if (operation.equalsIgnoreCase("Verify")) { System.out.println("Verify--->" +temp); temp.isDisplayed(); } } public WebElement getElement(String locator, String objectName) throws Exception { WebElement temp = null; System.out.println("Locator-->" + locator); if (locator.equalsIgnoreCase("id")) { temp = driver.findElement(By.id(objectName)); } else if (locator.equalsIgnoreCase("xpath")) { temp = driver.findElement(By.xpath(objectName)); System.out.println("xpath temp ----->" + temp); } else if (locator.equalsIgnoreCase("name")) { temp = driver.findElement(By.name(objectName)); } return temp; } } 

公共类WritableData {

 Workbook wbook; WritableWorkbook wwbCopy; String ExecutedTestCasesSheet; WritableSheet shSheet; public WritableData(String testSuitePath, String OBJECT_REPOSITORY_PATH) throws RowsExceededException, WriteException { // TODO Auto-generated constructor stub try { wbook = Workbook.getWorkbook(new File(testSuitePath)); wwbCopy= Workbook.createWorkbook(new File(testSuitePath),wbook); System.out.println("writable workbookC-->" +wwbCopy); shSheet=wwbCopy.getSheet("Login"); System.out.println("writable sheetC-->" +shSheet); //shSheet = wwbCopy.createSheet("Login", 1); } catch (Exception e) { // TODO: handle exception System.out.println("Exception message" + e.getMessage()); e.printStackTrace(); } } public void shSheet(String strSheetName, int iColumnNumber, int iRowNumber, String strData) throws WriteException, IOException { // TODO Auto-generated method stub WritableSheet wshTemp = wwbCopy.getSheet(strSheetName); shSheet=wwbCopy.getSheet("Login"); WritableFont cellFont = null; WritableCellFormat cellFormat = null; if (strData.equalsIgnoreCase("PASS")) { cellFont = new WritableFont(WritableFont.TIMES, 12); cellFont.setColour(Colour.GREEN); cellFont.setBoldStyle(WritableFont.BOLD); cellFormat = new WritableCellFormat(cellFont); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); } else if (strData.equalsIgnoreCase("FAIL")) { cellFont = new WritableFont(WritableFont.TIMES, 12); cellFont.setColour(Colour.RED); cellFont.setBoldStyle(WritableFont.BOLD); cellFormat = new WritableCellFormat(cellFont); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); } else { cellFont = new WritableFont(WritableFont.TIMES, 12); cellFont.setColour(Colour.BLACK); cellFormat = new WritableCellFormat(cellFont); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); cellFormat.setWrap(true); } Label labTemp = new Label(iColumnNumber, iRowNumber, strData, cellFormat); shSheet.addCell(labTemp); System.out.println("writableSheet---->"+shSheet); wwbCopy.write(); wwbCopy.close(); wbook.close(); 

}}

您可以通过复制现有的Excel,然后根据需要进行修改来创buildExcel。

  Workbook existingWorkbook = Workbook.getWorkbook(new File(Constant.wBook)); WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(Constant.wBook), existingWorkbook); WritableSheet sheetToEdit = workbookCopy.getSheet("report"); int step_num = col + 1; Label lb = new Label(0, col, Integer.toString(step_num)); Label lb1 = new Label(1, col, text); Label lb2 = new Label(2, col, result); System.out.println(text + "" + result + ""); sheetToEdit.addCell(lb); sheetToEdit.addCell(lb1); sheetToEdit.addCell(lb2); workbookCopy.write(); workbookCopy.close(); existingWorkbook.close();