سؤال

I have developed this script where i write on an xls worksheet.

var excelFile = "C:\\TestActiveX\\Test.xls";
function readExcelFileAndUpdateView()
{
    var w =new ActiveXObject("Excel.Application");      
    w.Workbooks.Open(excelFile);  
    var objWorksheet1 = w.Worksheets(1);    
    var name =objWorksheet1.Cells(1,2);
    if(name==null){
        name="";
    }

    var objWorksheet2 = w.Worksheets(2);    
    var surname = objWorksheet2.Cells(1,2);
    if(surname==null){
        surname="";
    }

    var str = "Name is :"+name+"<br/>Surname is :"+surname;

    document.getElementById("txtAreaXLS").innerHTML=str;    
    w.Application.Quit();
    w.Quit();
    w = null;               
}

The problem is that when this function finishes execution, I have 2 EXCELL objects running in my Windows Task Manager. Releasing resources at the end of my function doesn't seem to be working.

I have also tried objWorksheet1.close(true); and w.Workbooks.Close(true); and w.ActiveWorkbook.Close(true); with no luck.

Does anyone have a clue which are the open Objects, and how can I get rid of them?

هل كانت مفيدة؟

المحلول

ActiveX can have some odd behavior. You need to make sure that you set all references to the ActiveX object (and sub-objects/members) to null. In your code, you have set w = null, but you still have references to objWorksheet1, name, objWorksheet2, surname. Set those to null too. I would recommend a try/catch/finally block where you null those in the finally block.

There is a good article on CodeProject that recommends: Save, Close, Quit, null. Still, that won't matter unless you have nulled all references to the ActiveX objects & its members.

Sections 12 and 13 of this article have some good examples: http://www.codeproject.com/Articles/404688/Word-Excel-ActiveX-Controls-in-ASP-NET

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top