使用Selenium通过POI将从文本框中获取的值写入Excel列

时间:2016-05-25 10:26:49

标签: java selenium selenium-webdriver

我从文本框中获取了值并存储在字符串中。现在我想将存储在string中的这个值写入excel文件中名为'Username'的列中。 E.G:从文本框中获取用户名为“Test1”,并希望在Excel的“用户名”列中写入。我正在使用POI使用Selenium编写excel文件。

2 个答案:

答案 0 :(得分:0)

嗨请执行如下逻辑

public static void writeExcel(String filePath,String fileName,String sheetName,String [] dataToWrite)抛出IOException {

        //Create a object of File class to open xlsx file
        File file =    new File(filePath+"\\"+fileName);
        //Create an object of FileInputStream class to read excel file
        FileInputStream inputStream = new FileInputStream(file);
        Workbook Workbook = null;
        //Find the file extension by spliting file name in substing and getting only extension name
        String fileExtensionName = fileName.substring(fileName.indexOf("."));
        //Check condition if the file is xlsx file
        if(fileExtensionName.equals(".xlsx")){
        //If it is xlsx file then create object of XSSFWorkbook class
        Workbook = new XSSFWorkbook(inputStream);
        }

        //Check condition if the file is xls file
        else if(fileExtensionName.equals(".xls")){
            //If it is xls file then create object of XSSFWorkbook class
            Workbook = new HSSFWorkbook(inputStream);
        }  

    //Read excel sheet by sheet name    
    Sheet sheet = Workbook.getSheet(sheetName);
    //Get the current count of rows in excel file
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
    //Get the first row from the sheet
    Row row = sheet.getRow(0);
    //Create a new row and append it at last of sheet
    Row newRow = sheet.createRow(rowCount+1);
    //Create a loop over the cell of newly created Row
    for(int j = 0; j < row.getLastCellNum(); j++){
        //Fill data in row
        Cell cell = newRow.createCell(j);
        cell.setCellValue(dataToWrite[j]);

    }
    //Close input stream
    inputStream.close();
    //Create an object of FileOutputStream class to create write data in excel file
    FileOutputStream outputStream = new FileOutputStream(file);
    //write data in the excel file
    Workbook.write(outputStream);
    //close output stream
    outputStream.close();
    }

现在在主要方法中调用上面的内容,如下面的

 public static void main(String[] args) {
            // TODO Auto-generated method stub
            WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            String valueToWrite = "test 1";
            //Create an object of current class
            WriteExcelFile objExcelFile = new WriteExcelFile();
            //Write the file using file name , sheet name and the data to be filled
            objExcelFile.writeExcel(System.getProperty("user.dir")+"\\src\\excelExportAndFileIO","ExportExcel.xlsx","ExcelDemo",valueToWrite);

        }

答案 1 :(得分:0)

您需要知道&#34;用户名&#34;的列号。 excel表中的列。一旦你知道了那么你就可以很容易地写出你从网页上捕获的String的值。你可以采取以下方法 -

        File excelFile = new File("C:\\path\\of\\excel\\file\\excel.xlsx");
        String cellNo = 3; //column number of "UserName" column
        String rowNo = 1; // row number 
        String userName = "Test1"; // username fetched from textbox

        FileInputStream fis = new FileInputStream(excelFile);
        XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(rowNo); 
        row.createCell(cellNo).setCellValue(userName);
        fis.close();
        FileOutputStream fos = new FileOutputStream(excelFile);
        workbook.write(fos);
        fos.close();

这是一个非常简单的方法,仅用于此目的,并不是针对任何其他任务推广的方法。 您可以查看simple script that reads from a excel sheet and writes back to same excel sheet using Apache POI, here.

此致 Punkaaj