private void Button1_Click(object sender, System.EventArgs e)
{
     Excel.Application myExcel = new Excel.ApplicationClass();
     Excel.Workbooks xBks=myExcel.Workbooks;
     Excel._Workbook  xBk=xBks.Add(true);
     Excel._Worksheet xst=(Excel._Worksheet)myExcel.ActiveSheet;
    
     //xst.Cells[1,4]="普通报表";
     NAR(xst);
     xBk.Close(false,null,null);
     NAR(xBk);
     NAR(xBks);
     myExcel.Quit();
     NAR(myExcel);
     GC.Collect(); }private void NAR(object o)
{
    try
    {
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    }
    catch {}
    finally
    {
         o = null;
    }
}以上是一个将数据写入Excel程序,可是我发现将如果将上面注释掉的那一行取消注释,程序运行后就不能有效的关闭Excel.exe进程,不知道这个程序该怎样修改阿。

解决方案 »

  1.   

    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    后再加上
    o=null;
    试试看吧。我也是瞎猜
      

  2.   

    I don't use automation much, but can you call NAR on Cells object? or since you call xst.Cells[1,4]="普通报表";you need to use an extra NAR(xst)?Excel._Worksheet xst=(Excel._Worksheet)myExcel.ActiveSheet;
    xst.Cells[1,4]="普通报表";
    NAR(xst);
    NAR(xst);but really, you should not use office automation in asp.net, see
    INFO: Considerations for Server-Side Automation of Office
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q257757