C# 如何杀死Excel

C# 如何杀死Excel,c#,winforms,multithreading,excel,timer,C#,Winforms,Multithreading,Excel,Timer,我在控制台应用程序X中有一个c#方法,这会启动一个进程;控制台应用程序Y(使用相同的c#解决方案编写)。 然后,应用程序Y在Excel 2010工作簿中激发vba宏 为了在wkbook VBA中进行测试,我添加了一些代码来强制执行运行时错误1004 winForm使用使用Forms计时器触发的进程事件终止进程。它正在按程序工作,我只是想让它多做一点。 为什么在我终止进程时,XL实例在发现错误时仍然保持打开状态?如果XL实例仍然存在,当它终止进程时,我如何找到一种方法来摆脱它,然后将错误消息发回我

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

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

winForm使用使用Forms计时器触发的进程事件终止进程。它正在按程序工作,我只是想让它多做一点。 为什么在我终止进程时,XL实例在发现错误时仍然保持打开状态?如果XL实例仍然存在,当它终止进程时,我如何找到一种方法来摆脱它,然后将错误消息发回我的winForm

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


这可能是报表计划程序的问题,它没有关闭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的帮助,但主要是我的一个好朋友的帮助,不幸的是,他没有注册。Excel似乎已经死了!。另外,他对表单进行了编码,这样表单就会传回一个指示器,告诉表单是否存在excel问题。还为我们提供了为每个excel进程构建最大运行时间的选项

他使用以下方法帮助清除Excel

1.在调度程序中

  • 把计时器移到那里
  • 在vba中没有错误的情况下执行excel清理代码,在达到最大执行时间的相反情况下执行excel清理代码(使用Kill方法)
  • 如果excel正常完成,则从计划程序返回0到窗体应用程序;如果excel被终止,则从计划程序返回1
2.在窗体应用程序中,分析ProcessExited事件处理程序和enable按钮等中调度程序的返回值

因此,新的调度程序:

 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();
        }
    }
  }

我读了书名,并认为是另一个人恨微软。我认为书名的改变可能会有帮助,谢谢安德鲁-目前还不接近代码,但我明天就开始玩这个。我的朋友还建议我上面介绍的代码还可以,问题更可能是ReportScheduler_v3.exe及其处理xl的方式
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();
        }
    }
  }