2016-05-25 3 views
0

zu holen Ich habe den Wert aus dem Textfeld abgerufen und in einer Zeichenfolge gespeichert. Jetzt möchte ich diesen in string gespeicherten Wert in die Spalte 'Benutzername' in der Excel-Datei schreiben. E.G: Holte den Benutzernamen aus dem Textfeld als 'Test1' und möchte dies in der Spalte 'Benutzername' des Excel schreiben. Ich verwende POI, um Excel-Datei mit Selenium zu schreiben.Um den Wert aus dem Textfeld in die Spalte von Excel durch POI mit Selenium

Antwort

0

hallo implementieren Sie bitte die Logik wie unter

public static void writeExcel (String filePath, String filename, String Sheetname, String [] dataToWrite) throws 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(); 
    } 

nun die oben rufen in Haupt Methode wie unter

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

     } 
+0

@ Rajnish- was in der (filePath + "\\" + filename) aufgenommen werden: nur der Dateiname ("C: \\ Users \ \ Nishant \\ Downloads \\ TestData.xls ") wird nur – Roy

+0

Ihr Pfad zur Datei +" \\ "+ Excel-Blattname (dataSheet.xls) hinzugefügt –

0

Sie müssen die Spaltennummer der Spalte "Benutzername" in der Excel-Tabelle kennen. Sobald Sie wissen, dass es dann einfach ist, den Wert von String, den Sie von der Webseite erfasst haben, zu schreiben. Sie können unter Ansatz -

 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(); 

Diese sehr einfache Ansatz für diesen Zweck gerade ist und man nicht für jede andere Aufgabe verallgemeinert. Sie können simple script that reads from a excel sheet and writes back to same excel sheet using Apache POI, here.

Grüße einen Blick darauf werfen, Punkaaj

Verwandte Themen