Beispiel #1
0
 /// <summary> 获取全局的 Excel 程序 </summary>
 /// <param name="visible"></param>
 /// <returns>获取失败则返回 null</returns>
 public static Application GetExcelApp(bool visible = false)
 {
     if (_workingApp != null)
     {
         var processId = 0;
         var threadId  = Windows.GetWindowThreadProcessId(_workingApp.Hwnd, ref processId);
         var pr        = Process.GetProcessById(processId);
         if (pr == null || pr.HasExited)
         {
             _workingApp = null;
         }
         else
         {
             _workingApp.Visible = visible;
             return(_workingApp);
         }
     }
     if (_workingApp == null)
     {
         _workingApp = new Application {
             Visible = visible
         };
     }
     if (_workingApp == null)
     {
         throw new NullReferenceException($"无法打开 Excel 程序!");
     }
     return(_workingApp);
 }
Beispiel #2
0
 /// <returns>成功则返回 true</returns>
 public static bool KillActiveExcelApp(Application appToKill)
 {
     if (appToKill != null)
     {
         try
         {
             // excelApp.Quit();
             var processId = 0;
             var threadId  = Windows.GetWindowThreadProcessId(appToKill.Hwnd, ref processId);
             var pr        = Process.GetProcessById(processId);
             pr.Kill();
             //
             if (appToKill.Equals(_workingApp))
             {
                 _workingApp = null;
             }
         }
         catch (Exception)
         {
             return(false);
         }
     }
     return(true);
 }