18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to export table values into excel file ?

kplokeshkplokesh Members
edited January 2012 in Sahi - Open Source
Hi

When I am trying to export table data using the following code I am getting error like

ERROR
Wrapped java.lang.NullPointerException...

var $data=_getText("_table(29)");
_writeFile($data,"C:/Users/lokeshk/Desktop/ab.xlsx");

Could you please guide me on this scenario ?

Thanks
Lokesh

Comments

  • Hi,

    Can any reply for above issue ?

    Thanks,
    Lokesh.
  • Hi Lokesh,

    Here is the example:
    _navigateTo("http://sahi.co.in/demo/tableTest.htm");
    <browser>
    function getTableContents(table){
        var array2d = new Array();
        var rows = table.rows;
        for (var i=0; i<rows.length; i++){
            var row = rows[i];
            array2d[i] = new Array();
            var cells = rows[i].cells;
            for (var j=0; j<cells.length; j++){
                array2d[i][j] = _getText(cells[j]);
            }
        }    
        return array2d;
    }
    </browser>
    
    _set($ar2d, getTableContents(_table("t2")));
    var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}; DBQ=D:\\trial.xls;readOnly=false", "", "");
    for(var $i=0; $i<$ar2d.length; $i++){
            $rs=db.update("INSERT INTO [Sheet1$] (item,price,list) VALUES('" + $ar2d[$i][0] + "','" + $ar2d[$i][1] + "','" + $ar2d[$i][2] + "')");
    }
    

    Create an excel sheet at D: drive with the following contents:

    Item Price List

    Now run the above script and check if this solves your issue.

    Regards
    Sumitra
  • Hi Sumitra,

    I have tried with your provided code but table data is not exporting and I am not getting any error also, can you please look on this ?

    Thanks,
    Lokesh.
  • Hi Lokesh,

    Is it possible for you to post your application URL here or point us to a sample application so that i can work on it and get back to you.

    Also, Kindly make sure that you are using the accessor identified by your application and make changes in the above code accordingly.

    Reagards
    Sumitra
  • Hi Sumitra,

    I have tested with demo test table only (ex:- http://sahi.co.in/demo/tableTest.htm).

    first if it's worked the I will change according my requirements.

    Thanks,
    Lokesh.
  • Hi Lokesh,

    The following code works for me:
    _navigateTo("http://sahi.co.in/demo/tableTest.htm");
    <browser>
    function getTableContents(table){
        var array2d = new Array();
        var rows = table.rows;
        for (var i=0; i<rows.length; i++){
            var row = rows[i];
            array2d[i] = new Array();
            var cells = rows[i].cells;
            for (var j=0; j<cells.length; j++){
                array2d[i][j] = _getText(cells[j]);
            }
        }    
        return array2d;
    }
    </browser>
    _set($ar2d, getTableContents(_table("t2")));
    var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}; DBQ=D:\\trial.xls;readOnly=false", "", "");
    for(var $i=0; $i<$ar2d.length; $i++){
        $rs=db.update("INSERT INTO [Sheet1$] (item,price,list) VALUES('" + $ar2d[$i][0] + "','" + $ar2d[$i][1] + "','" + $ar2d[$i][2] + "')");
    }
    

    The Excel sheet should contain:

    item price list

    Check if this solves your issue.

    Regards
    Sumitra
  • Hi Sumitra,

    I tried again with your code still problem not resolved

    Thx,
    lokesh.
  • Hi Lokesh,

    Could you please send us the excel sheet and the usage of script to support@sahi.co.in.

    Also, send me the screen shot of the table from where you are trying to extract the contents.

    Regards
    Sumitra
  • nagarnagar Members
    Hi Sumitra,

    I am having the same problem.

    I want to write Pass/Fail in Result column of my excel sheet, but not able to do the same.

    My Excel Sheet contains below columns. (columns are in Sheet2)
    SrNo Type Enabled Disabled Result
    0 div snatprofileid
    0 div appfilterid
    0 div webfilterid
    1 div snatprofileid
    1 div appfilterid

    This is how, I am trying:

    var $db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\\Documents and Settings\\shirish.nagar\\sahi\\userdata\\scripts\\Validations\\Validation_Book.xls;readOnly=false", "", "");

    $DB_FileValue1 = $db.select("select * from [Sheet1$]");
    $DB_FileValue2 = $db.select("select * from [Sheet2$]");


    $db.update("INSERT INTO [Sheet2$] (Result) VALUES('Pass') WHERE Disabled='snatprofileid' AND SrNo='0'");


    I have tried UPDATE statement also, but even that is not working.
    Kindly help in this issue.
  • Hi nagar,

    Please refer to the reply in the CRM.

    Regards,
    Theeran.
  • nagarnagar Members
    edited March 2012
    Hi Theeran,

    The .sah file attached with CRM reply does not describe how to insert value in a particular cell of a particular column.

    Kindly provide me the workaround, which can work on Sahi OS.
Sign In or Register to comment.