如何杀死Excel

本文关键字:Excel 何杀死 | 更新日期: 2023-09-27 18:28:19

我在控制台应用程序X中有一个c#方法,它启动了一个进程;控制台应用程序Y(用相同的c#解决方案编写)。然后,应用程序Y在Excel 2010工作簿中启动一个vba宏。

为了在VBA中进行测试,我添加了一些代码来强制执行运行时错误1004。

winForm使用进程事件来终止进程,该事件由Forms计时器触发。它正在按程序运行,我只想试着让它做得更多。为什么当我终止进程时,XL的实例在发现错误时保持打开状态?如果XL实例仍然存在,当它杀死进程时,我如何找到一种方法来删除它,然后将错误消息发布回我的winForm?

(ps下面的代码很熟悉,但问题不是重复的)

    private int elapsedTime;
    private Process p;
    private System.Windows.Forms.Timer myTimer;
    const int SLEEP_AMOUNT = 1000;//1s
    const int MAXIMUM_EXECUTION_TIME = 5000;//5s

    private void btRunReport_Click(object sender, EventArgs e) {
        btRunReport.Enabled = false;
        lbStatusUpdate.Text = "Processing..";
        //instantiate a new process and set up an event for when it exits
        p = new Process();
        p.Exited += new EventHandler(MyProcessExited);
        p.EnableRaisingEvents = true;
        p.SynchronizingObject = this;
        elapsedTime = 0;
        this.RunReportScheduler();
        //add in a forms timer so that the process can be killed after a certain amount of time
        myTimer = new System.Windows.Forms.Timer();
        myTimer.Interval = SLEEP_AMOUNT;
        myTimer.Tick += new EventHandler(TimerTickEvent);
        myTimer.Start();
    }
    private void RunReportScheduler() {
        p.StartInfo.FileName = @"''fileserve'department$'ReportScheduler_v3.exe";
        p.StartInfo.Arguments = 2;
        p.Start();
    }
    private void MyProcessExited(Object source, EventArgs e){
        myTimer.Stop();
        btRunReport.Enabled = true;
        lbStatusUpdate.Text = "Start";
    }
    void TimerTickEvent(Object myObject, EventArgs myEventArgs) {
        myTimer.Stop();
        elapsedTime += SLEEP_AMOUNT;
        if (elapsedTime > MAXIMUM_EXECUTION_TIME)
        {p.Kill();}
        else
        {myTimer.Start();}
    }

如何杀死Excel

这可能是报表调度程序的问题,它没有正确的方法关闭Excel。

这就是这样的方法:

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}

我保留了原始代码,但我使用了Andrew的帮助,但主要是我的一个好朋友的帮助,不幸的是,他没有注册SO。Excel似乎已经死了!。此外,他还以这样一种方式对其进行了编码,即它会传回一个指示符,告诉表单excel是否有问题。还为我们提供了为每个excel流程构建最大运行时间的选项。

他使用以下SO答案来帮助摆脱Excel

1.在调度器程序中

  • 将计时器移到那里
  • 在vba中没有错误的情况下实现excel清理代码,而在相反的情况下,当达到最大执行时间时(使用Kill方法)
  • 如果excel正常完成,则从调度程序返回0到表单应用程序;如果excel被终止,则返回1

2.在表单应用程序中,分析ProcessExited事件处理程序和启用按钮等中调度程序的返回值

因此,新的调度器:

 using System;
 using System.Text;
 using System.Runtime.InteropServices;
 using System.Diagnostics;
 using Excel = Microsoft.Office.Interop.Excel;
 using System.Timers;

class Program
{
   private const int SLEEP_AMOUNT = 1000;
   private const int MAXIMUM_EXECUTION_TIME = 10000;
   private Excel.Application excelApp =null;
   private Excel.Workbook book =null;
   private Timer myTimer;
   private int elapsedTime;
   private int exitCode=0;
   [DllImport("user32.dll", SetLastError =true)]
   static extern uint GetWindowThreadProcessId(IntPtr hWnd,out uint lpdwProcessId);
   static int Main(string[] args)
    {
       Program myProgram = newProgram();
       myProgram.RunExcelReporting(1);
       return myProgram.exitCode;
    }

   void myTimer_Elapsed(object sender,ElapsedEventArgs e)
    {
       myTimer.Stop();
       elapsedTime += SLEEP_AMOUNT;
       if (elapsedTime > MAXIMUM_EXECUTION_TIME)
        {
            //error in vba or maximum time reached. abort excel and return 1 to the calling windows forms application
           GC.Collect();
           GC.WaitForPendingFinalizers();
           if (book != null)
            {
               book.Close(false,Type.Missing, Type.Missing);
               Marshal.FinalReleaseComObject(book);
               book =null;
            }
           if (excelApp != null)
            {
               int hWnd = excelApp.Hwnd;
               uint processID;
               GetWindowThreadProcessId((IntPtr)hWnd,out processID);
               if (processID != 0)
                   Process.GetProcessById((int)processID).Kill();
                excelApp =null;
                exitCode = 1;
            }
        }
       else
        {
            myTimer.Start();
        }
    }

   void RunExcelReporting(int x)
    {
        myTimer =new Timer(SLEEP_AMOUNT);
        elapsedTime = 0;
        myTimer.Elapsed +=new ElapsedEventHandler(myTimer_Elapsed);
        myTimer.Start();
       try{
            excelApp =new Excel.Application();
            excelApp.Visible =true;
            book = excelApp.Workbooks.Open(@"c:'jsauto.xlsm");
            excelApp.Run("ThisWorkbook.rr");
            book.Close(false,Type.Missing, Type.Missing);
        }
        catch (Exception ex){
           Console.WriteLine(ex.ToString());
        }
       finally
        {
           //no error in vba and maximum time is not reached. clear excel normally
           GC.Collect();
           GC.WaitForPendingFinalizers();
           if (book != null)
            {
               try {
                    book.Close(false,Type.Missing, Type.Missing);
                }
                catch { }
               Marshal.FinalReleaseComObject(book);
            }
           if (excelApp != null)
            {
               excelApp.Quit();
               Marshal.FinalReleaseComObject(excelApp);
               excelApp =null;
            }
        }
    }
}

以及新的表单应用程序:

public partial class Form1 : Form
{
   SqlDataAdapter myAdapt = null; 
   DataSet mySet =null; 
   DataTable myTable =null; 
   public Form1()
    { InitializeComponent();}
    privatevoid Form1_Load(object sender,EventArgs e){ 
        InitializeGridView();
    }
   private Process myProcess;
   private void btRunProcessAndRefresh_Click(object sender,EventArgs e)
    {
        myProcess =new Process();
        myProcess.StartInfo.FileName =@"c:'VS2010Projects'ConsoleApplication2'ConsoleApplication4'bin'Debug'ConsoleApplication4.exe";
        myProcess.Exited +=new EventHandler(MyProcessExited);
        myProcess.EnableRaisingEvents =true;
        myProcess.SynchronizingObject =this;
        btRunProcessAndRefresh.Enabled =false;
        myProcess.Start();
    }
    privatevoid MyProcessExited(Object source,EventArgs e)
    {
        InitializeGridView();
        btRunProcessAndRefresh.Enabled =true;
       if (((Process)source).ExitCode == 1)
        {
           MessageBox.Show("Excel was aborted");
        }
       else
        {
           MessageBox.Show("Excel finished normally");
        }
    }
   private void btnALWAYSWORKS_Click(object sender,EventArgs e) { 
        InitializeGridView();
    }
    privatevoid InitializeGridView() { 
      using (SqlConnection conn =new SqlConnection(@"Data Source=sqliom3;Integrated Security=SSPI;Initial Catalog=CCL"))
        {
        myAdapt =new SqlDataAdapter("SELECT convert(varchar(25),getdate(),120) CurrentDate", conn);
        mySet =new DataSet();
        myAdapt.Fill(mySet,"AvailableValues"); 
        myTable = mySet.Tables["AvailableValues"];
        this.dataGridViewControlTable.DataSource = myTable;
        this.dataGridViewControlTable.AllowUserToOrderColumns =true;
        this.dataGridViewControlTable.Refresh();
        }
    }
  }