2015-09-08 25 views
2

我在Excel進程ID殺IM問題使用Excel互操作Excel進程

第一種情況,當我打開Excel文件,而我的程序訪問Excel中的數據,我打開Excel進程文件是訪問程序的相同過程。所以當我殺死進程ID時,我使用程序打開的另一個excel會被殺死。

第二種情況,如果我不殺死excel進程ID,當我嘗試使用用戶在我的程序訪問之前打開的進程時,當我的程序工作時,如果用戶關閉了excel,我的程序將終止。

是否有一種方法使用新進程並僅限制我的工作簿使用,如果其他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(); 
     } 
+1

Excel互操作雜亂。考慮使用http://epplus.codeplex.com/ –

+0

可以給我更多關於epplus plz; D @EricJ的信息。 – Kenz

+0

我現在更新了我的代碼,請給我更多評論@jstreet – Kenz

回答

1

您應該釋放退出子辦公流程之前互操作相關聯的每個對象。這是如此痛苦,所以我會建議你找到替代解決方案。