I'm using Interop Excel and my code works fine
class GeneralData
{ public static string excelPath = System.Windows.Forms.Application.StartupPath + @"\SteamProp";
public static ExcelApp._Application oExcel;
static ExcelApp.Workbooks oBooks;
public static ExcelApp._Workbook oBook;
static object oMissing = System.Reflection.Missing.Value;
// Function will be used in Main Form Load Method
public static void OpenExcelConnection()
{
oExcel = new ExcelApp.Application();
oExcel.Visible = false;
oBooks = oExcel.Workbooks;
oBook = oBooks.Open(excelPath, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
}
public static void ReleaseExcel()
{
try
{
oBook.Close(true, excelPath, oMissing);
oExcel.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBooks);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oExcel);
oBook = null;
oBooks = null;
oExcel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch
{ }
}
I made (OExcel) static to use it from different forms. I open the (OExcel) and use it then close at the end by calling the ReleaseExcel() Method. The code work fine, but if there was any different excel file opened from outside the application, the file which used in the C# application appears to the user even that :
oExcel.Visible = false;
and when the ReleaseExcel() method called this file doesn't close, and if i closed the C# application and opened it again the next Exception is raised:
The Microsoft Jet database engine cannot open the file ". It is already opened exclusively by another user, or you need Permission to view its data.
So Could you help me.