Data is removed from column 1 when writing data in column 2 using selenium webdriver

Here is my code:

    public static String[] array1 = {"devu","xyz","test","bb","run"};
    public static String[] array2 = {"dvu","yz","tet","b","run"};


 String excelFileName = "C:/Users/admin/IdeaProjects/Google_Demo/Write1.xlsx";//name of excel file

        String sheetName = "Sheet1";//name of sheet

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName);

        //iterating r number of rows
        for (int r = 0; r < 5; r++) {
            XSSFRow row = sheet.createRow(r);

            //iterating c number of columns
            for (int c = 0; c < 1; c++) {
                if (!row.equals(null)) {
                XSSFCell cell = row.createCell(c);

                cell.setCellValue(array1[r]);

            }
        }}



        int count1 = 0;
        for (int r = 0; r < 5; r++) {
            XSSFRow row1 = sheet.createRow(r);


            //iterating c number of columns
            for (int c = 2; c < 3; c++) {

                if (row1.equals(null)) {
                    row1 = sheet.createRow(count1);
                }
                    XSSFCell cell = row1.createCell(c);


                    cell.setCellValue(array2[r]);
                    count1++;

                }


            }

            FileOutputStream fileOut1 = new FileOutputStream(excelFileName);

            //write this workbook to an Outputstream.
            wb.write(fileOut1);
            //fileOut.flush();
            fileOut1.close();
        }

      

I took 2 arrays and wrote down the data to do well using Selenium web editor and POI.

It works great. If I write the data of the first array only, but as soon as the second array loop starts, it deletes the data from the first excel column, and only writes the data of the second array to the new column.

+3


source to share


1 answer


This is because you are creating a new one again row

in the second for loop

. The below line must be removed for your code to work according to the declared behavior -

XSSFRow row1 = sheet.createRow(r);

      



Following is the complete updated code -

public static String[] array1 = {"devu","xyz","test","bb","run"};
public static String[] array2 = {"dvu","yz","tet","b","run"};
String excelFileName = "C:/Users/admin/IdeaProjects/Google_Demo/Write1.xlsx";//name of excel file

String sheetName = "Sheet1";//name of sheet

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName);
//iterating r number of rows
for (int r = 0; r < 5; r++) {
    XSSFRow row = sheet.createRow(r);
    //iterating c number of columns
    for (int c = 0; c < 1; c++) {
            XSSFCell cell = row.createCell(c);
            cell.setCellValue(array1[r]);

    }
}
for (int r = 0; r < 5; r++) {
    XSSFRow row = sheet.getRow(r);
    // As per current input and code, this if block will never execute. 
    // However, adding it so that later, if first for loop will change, this code can work.
    if(row == null){
        row = sheet.createRow(r);        
    } 
    //iterating c number of columns
    for (int c = 2; c < 3; c++) {
        XSSFCell cell = row1.createCell(c);
        cell.setCellValue(array2[r]);
    }
}

FileOutputStream fileOut1 = new FileOutputStream(excelFileName);
//write this workbook to an Outputstream.
wb.write(fileOut1);
//fileOut.flush();
fileOut1.close();

      

+2


source







All Articles