Forum Discussion

sarya's avatar
sarya
Frequent Contributor
14 years ago

Script Code not opening .xlsx files

Hi,



I need to work with .xlsx files.I am using this code to open .xlsx files.It opens .xls files perfectly fine but not .xlsx files. In the local directory ,it creates a temporary .xlsx file also but then it does not open up the file .



function ExcelComp(fileName1,fileName2)

{

  var oExcel = Sys.OleObject("Excel.Application");

  var aSheets = Sys.OleObject("Scripting.Dictionary");

   var oSheets;

  oExcel.Workbooks.Close();

  try{

  oExcel.Workbooks.Open(fileName); 

    }

  catch(e)

  {

  Log.Error("Exception occured",e.description);

  return null ;

  }

   oExcel.Quit();

}



Thanks,

Sumedha

6 Replies

  • karkadil's avatar
    karkadil
    Valued Contributor
    In your code example, I can see that you are trying to open file using variable fileName (oExcel.Workbooks.Open(fileName)) whereas you pass to function 2 different parameters (fileName1,fileName2).



    Might it be a reason? :)
  • sarya's avatar
    sarya
    Frequent Contributor
    Hi Gennadiy,



    Thanks for the reply.Sorry for the earlier script but this one is simple script that tries to open an xlsx file,This too is not working.I am not able to figure out why is it not opening up.




    function test( )

    {

    var oExcel = Sys.OleObject("Excel.Application");

    var aSheets = Sys.OleObject("Scripting.Dictionary");

    var oSheets;

    oExcel.Workbooks.Close();

    try{

    oExcel.Workbooks.Open("C:\\Bur64.xlsx");

    }

    catch(e)

    {

    Log.Error("Exception occured",e.description);

    return null ;

    }

    //oExcel.Quit();

    }



    Thanks,

    Sumedha




    test( ){ oExcel = .OleObject("Excel.Application"); aSheets = .OleObject("Scripting.Dictionary"); oSheets;{ oExcel.Workbooks.Open("C:\\Bur64.xlsx"); } (e) { .Error("Exception occured",e.description); ; }//oExcel.Quit();}Thanks,Sumedha
  • Hi Sumedha,


    Add the following line after 'var oSheets;' - this can help you see an error message if it is displayed:




    oExcel.Visible = true;


    For more information, refer to the 'OleObject Property' help topic. If this suggestion does not help, please let us know which version of Excel you are using and send us the file you are trying to open.

  • sarya's avatar
    sarya
    Frequent Contributor
    Hi Allen,



    Thanks for the reply.The xlsx files opens up fine after including that line of code.However the other thing I am doing is comparing the xlsx files in the below code. The code below works fine for xls files but when I compare .xlsx files(excel 2007 files) ,it seems to take forever as if it is doing nothing.I am also including the sample .xlsx file that I am using for comparison.



    Thanks,

    Sumedha



    ExcelComp("C:\\Bur64.xlsx","C:\\tcfiles\\burster\\Excel\\Bur64.xlsx");


    function ExcelComp(fileName1,fileName2)

    {

    var oExcel = Sys.OleObject("Excel.Application");

    var aSheets = Sys.OleObject("Scripting.Dictionary");

    oExcel.Visible = true;

    var aInfo1 = createInfo(oExcel, fileName1, aSheets);

    // var fileName = "C:\\Temp\\book1.xls";

    var aInfo2 = createInfo(oExcel, fileName2, aSheets);

    var oResult = compare(aInfo1, aInfo2);

    try

    {

    if (oResult.length > 0)

      {Log.Warning(oResult);}

    else

      {Log.Message("The books are the same.");}

    }


    catch(e)

    {

    Log.Error("Exception occured",e.description);

    return null ;

    }

    oExcel.Quit();

    }


     



    function createInfo(oExcel, fileName, aSheets)


    createInfo(oExcel, fileName, aSheets)

    {


    var oSheets;


    oExcel.Workbooks.Close();


    try{


    oExcel.Workbooks.Open(fileName); //Exception occurs at this point when it tries to find a non-existing excel file


    }


    catch(e)


    {


    Log.Error("Exception occured",e.description);


    return null ;


    }


    oSheets = oExcel.Worksheets;


    sheetCount = oSheets.Count;


    if (aSheets.Count == 0)


    {


    for (var sheetId = 1; sheetId < sheetCount + 1; sheetId++)


    {


    var oSheet = oSheets.Item(sheetId);


    aSheets.Add(oSheet.Name, oSheet.Name);


    }


    }


    var vSheets = Sys.OleObject("Scripting.Dictionary");


    for (var sheetId = 1; sheetId < sheetCount + 1; sheetId++)


    {


    var oSheet = oSheets.Item(sheetId);


    if (aSheets.Item(oSheet.Name) == null)


    {


    continue;


    }


    var vSheet = Sys.OleObject("Scripting.Dictionary");


    var rowCount = oSheet.UsedRange.Rows.Count;


    for (var rowId = 1; rowId < rowCount + 1; rowId++)


    {


    var rowObj = oSheet.Rows.Item(rowId);


    var vRow = Sys.OleObject("Scripting.Dictionary");


    var cellCount = rowObj.Cells.Count;


    for (var cellId = 1; cellId < cellCount + 1; cellId++)


    {


    var cellObj = rowObj.Cells.Item(cellId);


    vRow.Add(cellObj.Column, cellObj.Text);


    }


    vSheet.Add(rowId, vRow);


    }


    vSheets.Add(oSheet.Name, vSheet);


    }


    return vSheets;


    }



    function compare(aInfo1, aInfo2)


    compare(aInfo1, aInfo2)

    {


    var strCompare = "";


    try


    {


    if (aInfo1.Count != aInfo1.Count)


    {


    strCompare = strCompare + "Wrong sheet count!"


    return strCompare;


    }


    }


    catch(e)


    {


    Log.Error("Exception occured",e.description);


    return null;


    }


    var vKeys = aInfo1.Keys().toArray();


    var sheetCount = aInfo1.Count;


    for (var sheetId = 0; sheetId < sheetCount; sheetId++)


    {


    if (!aInfo2.Exists(vKeys[sheetId]))


    {


    strCompare = strCompare + "Sheet names are different!";


    return strCompare;


    }


    var oSheet1 = aInfo1.Item(vKeys[sheetId]);


    var oSheet2 = aInfo2.Item(vKeys[sheetId]);


    var rowCount1 = oSheet1.Count;


    var rowCount2 = oSheet2.Count;


    if (rowCount1 != rowCount2)


    {


    strCompare = strCompare + "Wrong used row count in the '" + vKeys[sheetId] + "' sheet."


    return strCompare;


    }


    for (var rowId = 0; rowId < rowCount1; rowId++)


    {


    var rowObj1Key = oSheet1.Keys().toArray()[rowId];


    if (!oSheet2.Exists(rowObj1Key))


    {


    strCompare = strCompare + "The '" + rowObj1Key + "' row key was not found.";


    return strCompare;


    }


    var oRow1 = oSheet1.Item(rowObj1Key);


    var oRow2 = oSheet2.Item(rowObj1Key);


    var cellCount = oRow1.Count;


    for (var cellId = 0; cellId < cellCount; cellId++)


    {


    var celObj1Key = oRow1.Keys().toArray()[cellId]


    if (!oRow2.Exists(celObj1Key))


    {


    strCompare = strCompare + "The '" + celObj1Key + "' cell was not found." + "\r\n";


    return strCompare;


    }


    var oCell1 = oRow1.Item(celObj1Key);


    var oCell2 = oRow2.Item(celObj1Key);


    if (!SameText(oCell1, oCell2))


    {


    strCompare = strCompare + "The " + vKeys[sheetId] + ":\r\n The [" + rowId + ", " + cellId + "] cell was changed." + "\r\n" + " The '" + oCell1 + "' value." + "\r\n" + " The '" + oCell2 + "' value." + "\r\n";


    }


    }


    }


    }


    return strCompare;


    }

  • Hi,


    Replace the following line in the createInfo function:




      var rowObj = oSheet.Rows.Item(rowId);


    with the line below:




      var rowObj = oSheet.UsedRange.Rows.Item(rowId);


    Tell us whether it works.

  • sarya's avatar
    sarya
    Frequent Contributor
    Hi Allen,



    I made that change to the script and it worked fine as it might be looking into all the cells making it take too much time to evaluate differences.  Also I had some queries regarding the same script which I posted as a support portal message. It would be really great if  those doubts get solved .



    Thanks again for your help.

    Sumedha