Forum Discussion

dhanapal1's avatar
dhanapal1
Contributor
15 years ago

How to write data into excel sheet?

Hi 



      Right now I am doing scripting with c# am unable to write data into excel sheet.  I have tried with ole object and activex object. I have refered msdn too but I couldn't get  answer.  anyone please help me by providing with some examples.




function adddataexcel()

{

// Launch Excel

var app;

var book;

var sheet;

var i,j;

var val ="dpl007";

app= Sys["OleObject"]("Excel.Application");

//app = new ActiveXObject("Excel.Application");

book = app["Workbooks"]["Open"]("C:\\TestData\\dpl");

sheet = book["ActiveSheet"];



for
(j=0; j<=4; j++)

    sheet["Cells"](i,j).value= val;  //



book["Save"]();

app["Quit"]();

}


8 Replies

  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Guys,



    Have you tried using the excel file name with the suffix (.xls or .xlsx) ?



    Sincerely
  • Hi,



    You can find a sample script demonstrating how to write values to an Excel sheet here.
  • Hi,



    I'm trying to write to excel sheet using the following code but error keeps complaining about the file "C:\TestData\db1" couldn't be found. Althought i've created the file in C:\.






    function adddataexcel()



    {



    // Launch Excel



    var app;



    var book;



    var sheet;



    var i,j;



    var val ="dpl007";



    app= Sys["OleObject"]("Excel.Application");



    //app = new ActiveXObject("Excel.Application");



    book = app["Workbooks"]["Open"]("C:\\TestData\\dp1");



    sheet = book["ActiveSheet"];



    for(j=0; j<=4; j++)



    sheet["Cells"](i,j).value= val;



    book["Save"]();



    app["Quit"]();



    }






  • Hi,



    Yes, I did try to name the file with suffix "dpl.xls and dpl.xlsx" and still getting the same error:

    "'C:\TestData\dpl.xlsx' couldn't be found. Check the spelling of the file name, and verify that the file location is correct...etc..



    Is there something wrong i'm doing or should do?.



    Thanks for ur help..
  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Azeddin,



    Let's forget TestComplete for a moment and try something simpler. Does the following work for you from the command line (cmd.exe):



    dir C:\TestData\dpl.xlsx



    It should return the file's basic information (size, date) otherwise "File Not Found".



    Sincerely

  • Hi Simon,



    Yes, it works for me. Basic Info. such as Date, Time, and size(in bytes) are displayed in the command line window.



    Thanks,

    Az.
  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Azedding,



    Ok so the file exists :-)



    I copy-pasted the code at Working With Excel Files via COM and it worked just fine after creating the excel file. My tests were done with Excel already running with previously loaded files and with Excel not running before the test.



    As far as comparing your code to:



    function ReadDataFromExcel()

    {

      var Excel = Sys["OleObject"]("Excel.Application");

      Excel["Workbooks"]["Open"]("C:\\MyFile.xls");



      var RowCount = Excel["ActiveSheet"]["UsedRange"]["Rows"]["Count"];

      var ColumnCount = Excel["ActiveSheet"]["UsedRange"]["Columns"]["Count"];



      for (var i = 1; i <= RowCount; i++)

      {

        var s = "";

        for (var j = 1; j <= ColumnCount; j++)

          s = s + VarToString(Excel["Cells"](i, j)) + Chr(13) + Chr(10)

        Log["Message"]("Row: " + VarToString(i), s);

      }



      Excel["Quit"]();



    }



    It is not necessary to activate the workbook and the way the cell value is fetched is different.



    So please try to code above because I am starting to run out of ideas.



    Sincerely
  • for(j=0;j<4;j++)

    {


    sheet["Cells"](i,j).Value=12;

    }



    I am getting an error saying ; expected. Can anyone help me with this