C# Excel

L

Luigi

Sorry for my bad english.
I have this problem.
I need open and close a file of Excel
because I have to calculate some
functions on a sheet.
This class:

//....................
//....................
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

internal class OpenExcel
{
Microsoft.Office.Interop.Excel.Application objApp;
Microsoft.Office.Interop.Excel._Workbook objBook;
Microsoft.Office.Interop.Excel.Workbooks objBooks;

public void ExOpen(string PathFile)
{
try
{
objApp = new Microsoft.Office.Interop.Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Open(PathFile,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
objApp.DisplayAlerts = false;
objBook.Save();
objApp.DisplayAlerts = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
objBook.Close(false, PathFile, null);
objApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
}
}

}

works ok, but I see the process in task manager.
What's the matter?
Thank you.

Luigi.
 
P

Patrice

Hi,

What if you try to release the objects as soon as you are done with them i.e
:

System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks); //
Or even earlier or don"t use it as you are using this a single time...
objBook.Close(false, PathFile, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);
objApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);

Note sure but if I remember you have this problem when you try to close the
app but still have references to subordinate objects...
 
F

filip

Sorry for my bad english.
I have this problem.
I need open and close a file of Excel
because I have to calculate some
functions on a sheet.
This class:

//....................
//....................
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

internal class OpenExcel
{
    Microsoft.Office.Interop.Excel.Application objApp;
    Microsoft.Office.Interop.Excel._Workbook objBook;
    Microsoft.Office.Interop.Excel.Workbooks objBooks;

    public void ExOpen(string PathFile)
    {
        try
        {
            objApp = new Microsoft.Office.Interop.Excel.Application();
            objBooks = objApp.Workbooks;
            objBook = objBooks.Open(PathFile,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);
            objApp.DisplayAlerts = false;
            objBook.Save();
            objApp.DisplayAlerts = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            objBook.Close(false, PathFile, null);
            objApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
        }
    }

}

works ok, but I see the process in task manager.
What's the matter?
Thank you.

Luigi.

You can try working with Excel form C# by using
http://www.gemboxsoftware.com/GBSpreadsheet.htm
Here's a list of reasons why it's better to use it then excel interop:
http://www.gemboxsoftware.com/LA/Excel-Automation-and-Excel-Interop.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top