Search code examples
javawebdriverapache-poiselenium-webdriverjxl

Append excel cell contents to new excel file


I have a piece of code that reads the contents and pastes it into the new excel file

The code is

for(int rownum=2; rownum<=datatable.getRowCount(testName); rownum++)
    for(int colnum=1; colnum<=datatable.getColumnCount(testName); colnum++) {
        datatable.getCellData(testName, "Serial", rownum);
        // Get value of other column  Serial :)
        String getdata = datatable.getCellData(testName, "Rules", rownum);
        if (datatable.getCellData(testName, "Serial", rownum).equals(serial)) {
            datatable.getCellData(testName, colnum, rownum);
            datatable.setCellData(testName, "Result", rownum,"PASS" );
            datatable = new Xls_Reader(System.getProperty("user.dir")+"//src//config//Result.xlsx");
            datatable.setCellData("Data Sheet", "Results", rownum,"PASS" );
            datatable.setCellData("Data Sheet", "Summary", rownum, getdata);
        }
    }

Now this is passed as (String ,string) and this is for a single test case that run. The problem is when I insert the Contents to Result it will be stored in at cell 2, 3 . This will be overwritten again and again. I want to append all the multiple executed cases which is in multiple sheet and the Result must be appended. Is that possible ? Please help.


Solution

  • I'm going to do a wild guess, but I think I know what's going on.

    Before the start of this snippet, you have read in an XSL sheet, stored in the dataSheet variable. The for loops at the start of the snippet will look at the contents of the dataSheet variable to check the completion condition for every iteration.

    If the serial value is equal to some value, you do some things, then overwrite the datasheet variable with a new document, in which you proceed to store a few values. When the loop completes, it will look at that new datasheet whether the loop is complete, and as far as I can see will just go like this forever, replacing the original document on every iteration.

    In order to fix this, at least put the newly created data sheet in a separately named variable, and store the width and height of the original data sheet in variables before entering the loops so they're not altered during the execution of the loops. By the looks of it you're also fetching data from the data sheet without actually using it, or is that just me?