在c#中的Excel过程

我在使用Excel InterOp的excel进程ID查杀中遇到问题

第一种情况,当我打开excel文件,而我的程序访问Excel中的数据时,我打开excel文件的过程是访问程序的过程。 所以当我杀死进程ID,另一个我使用程序打开的Excel将被杀死。

第二种情况,如果我不杀死excel进程ID,当我尝试使用用户在我的程序访问之前打开的进程时,当我的程序工作,如果用户closuresexcel我的程序将终止。

有没有一种方法使用新的进程,只限制我的工作簿使用,如果其他excel已经打开,我怎么能让他们通过另一个进程打开?

这个我的代码,

public static void getExceltable() { Microsoft.Office.Interop.Excel.Application m_app; m_app = new Microsoft.Office.Interop.Excel.Application(); m_app.DisplayAlerts = false; object misValue = System.Reflection.Missing.Value; string filename = Core.Class1.importPath; Workbook workbook=m_app.Workbooks.Open(filename,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); Worksheet sheet = m_app.Sheets[1]; //// My Excel Process //// workbook.Close(); int id; GetWindowThreadProcessId(m_app.Hwnd, out id); Process excelProcess = Process.GetProcessById(id); m_app.Quit(); excelProcess.Kill(); } 

在退出子办公程序之前,您应该释放与互操作相关的每个对象。 这是如此痛苦,所以我会build议你find替代解决scheme。