Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my application i am using a third party tool to show an uploaded excel sheet and to edit and save it. During this process i need to delete the existing excel sheet and edit and save new excel sheet. I have tried to delete but the excel will not delete and throw error as excel sheet in use by another process cannot delete. So i decided to close that excel sheet or kill the process so i wrote code as below:
using Excel = Microsoft.Office.Interop.Excel;


Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;


C#
try
{

    _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    WB = _Excel.Workbooks.Open(@"FIENAME",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

    WB.Close(false , Type.Missing, Type.Missing);


    //do something

}
catch (Exception ex)
{
    WB.Close(false, Type.Missing, Type.Missing);

    throw;
}
finally
{
    GC.Collect();
    GC.WaitForPendingFinalizers();

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);


}


but this will still throw same error that the excel sheet is being used by another process... Can u please help me how can we close that excel or workbook programatically. Or what programatic solution this can have.. as killing process might effect other projects in Server...

Please help!! Thanking You in advance!!
Posted
Comments
ZurdoDev 14-Aug-13 7:26am    
You'll need to find a way to get the 3rd party tool to close it.
Maciej Los 14-Aug-13 7:36am    
Why?
ZurdoDev 14-Aug-13 7:42am    
Because OP said he got an error about it being in use when he tried to close it.
Maciej Los 14-Aug-13 7:55am    
Have you seen my comment to the OP's question?
ZurdoDev 14-Aug-13 8:03am    
Yes, isn't your code the same as the OPs? What am I missing? Of course you comment it out in the catch but otherwise what is different?

1 solution

First of all, please, read my comment to the question, then have a look here: Closing excel workbook from C#[^]
 
Share this answer
 
Comments
Shruthi.BT 16-Aug-13 6:59am    
Hey Hi.... this works in my loocal system but in Server it does not.. that is we use Windows Server 2008 and it is 64 bit.. but the Server does not have MS Office so it does not support the Interop that is the namespace...

Microsoft.Office.Interop.Excel;

How can i solve this... any other way i can close the process?? when i gave response.write it showed this error...

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154. at GetFiles.StartExcel()

how can we solve this....?? ANY IDEA ... plz Help!1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900