2016-05-25 9 views
0

を使用して、テキストボックスから取得した値をPOI経由でExcelの列に書き込むには、テキストボックスから値を取り出し、文字列に格納しました。今私は、この値を文字列に 'Username'という名前の列にExcelファイルに書き込む必要があります。 E.G:ユーザー名をテキストボックスから 'Test1'として取得し、これをExcelの 'Username'列に書きます。私はPOIを使ってSeleniumを使用してExcelファイルを作成しています。Selenium

答えて

0

パブリック静的ボイドwriteExcel(文字列filePathに、文字ファイル名、文字SHEETNAME、文字列[] 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); 

     } 
+0

@ Rajnish-(filePathに+ "\\" + fileNameに)に含まれるべきか:ファイル名のみ( "C:\\ユーザー\ \ Nishant \\ Downloads \\ TestData.xls ")だけが追加されます。 – Roy

+0

ファイルへのパス+" \\ "+ Excelシート名(dataSheet.xls) –

0

Excelシートの[ユーザー名]列の列番号を知る必要があります。一度それが分かれば、Webページから取得したStringの値を書き込むことは簡単になります。あなたはアプローチの下に取ることができます - これは非常に単純なアプローチは、まさにこの目的のためであり、他のタスクのための1つを一般化されていない

 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を取ることができ