关于用正常方式销毁excel进程的问题请教。
导出电子表格时,导出完成,我想销毁 excel 进程,我不想用杀死的办法,因为那样毕竟不好。我想通过正常的方式,当其引用为0时,自动销毁。以下代码就是导出电子表格的代码,导出完成后,销毁excel进程,但是为何
有一句代码要连接执行两次才能销毁呢?                    book.Close(false,miss,miss);
                    books.Close();
                    excel.Quit();                    NAR(sheet);
                    NAR(book);
                    NAR(books);                    
//以下必须清理两次才行,不知为什么
                    NAR(excel);
                    NAR(excel);
                    GC.Collect();
                    MessageBox.Show("数据已经成功导出!");        //消毁excel进程
        private static void NAR(object o)
        {
            try 
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            finally 
            {
                o = null;
            }
        }完整代码如下:        //DataGridView 导出到Excel
        public static void SaveAs(DataGridView gridView)
        {
            //导出到execl   
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "导出Excel (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                //saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出Excel格式文档";
                saveFileDialog.ShowDialog();
                string strName = saveFileDialog.FileName;
                if (strName.Length != 0)
                {
                    //toolStripProgressBar1.Visible = true;
                    //以下变量什么意思?
                    System.Reflection.Missing miss = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();                    excel.Application.Workbooks.Add(true);                    excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面
                    //excel.Visible = true;                    if (excel == null)
                    {
                        MessageBox.Show("EXCEL无法启动!(可能您没有安装EXCEL,或者版本与本程序不符)","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        return;
                    }                    Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
                    Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
                    Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
                    sheet.Name = "数据";                    //添加表头
                    for (int i = 0; i < gridView.ColumnCount; i++)
                    {
                        excel.Cells[1, i+1] = gridView.Columns[i].HeaderText.ToString();
                    }                    //填充数据
                    for (int i = 0; i < gridView.RowCount; i++)
                    {
                        //i为行,j为列
                        for (int j = 0; j < gridView.ColumnCount; j++)
                        {
                            ////注意:datagrid的引用方法是 datagrid1[列,行],即先列后行
                            if (gridView[j, i].Value.GetType() == typeof(string))
                            {
                                //MessageBox.Show(gridView[j, i].Value.ToString());
                                excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
                            }
                        }
                        //toolStripProgressBar1.Value += 100 / gridView.RowCount;
                    }                    sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                    //book.Close(false, miss, miss);
                    //books.Close();
                    //excel.Quit();
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);                    //下面销毁excel进程,为何无效呢?
                    book.Close(false,miss,miss);
                    books.Close();
                    excel.Quit();
                    //NAR(excel);                    NAR(sheet);
                    NAR(book);
                    NAR(books);
                    //以下必须清理两次才行,不知为什么
                    NAR(excel);
                    NAR(excel);                    GC.Collect();
                    //GC.WaitForPendingFinalizers();        //不知作用是什么,有人说也须加上,可是经试不加也可以,不知为什么?
                    MessageBox.Show("数据已经成功导出!");
                    //toolStripProgressBar1.Value = 0;
                    System.Diagnostics.Process.Start(strName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        //消毁excel进程
        private static void NAR(object o)
        {
            try 
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            finally 
            {
                o = null;
            }
        }

解决方案 »

  1.   

    NAR(excel);
    如果只写一行,则在进程中,总会有一个 excel 进程无法销毁,
    但是不会多于一个,即再执行导出又会生成一个 excel 进程,导出完毕后,第二个excel进程销失,但是第一个会永远存在。
      

  2.   

    你的到处,是使用 excel组件实现的,
    你调用完成后,记得把 对象关闭,释放资源,再结束进程,应该就可以了
      

  3.   

    我主要想问,为何要执行两次相同的代码,才能达到清理excel进程的效果。
      

  4.   

    我以前也遇到过,原因是:创建的每个对象都得释放掉,Excel进程才会关闭。参考代码:
    private void ExportExcel(DataTable dt, string _strTitle)
        {
            string newpath = Server.MapPath(".") + @"\" + Guid.NewGuid() + ".xls";
            _excel.Application app = new _excel.Application();
            _excel.Workbooks wbooks = app.Workbooks;
            _excel.Workbook wbook = wbooks.Add(System.Reflection.Missing.Value);        //VS2003中最好不要如下创建
            // _excel.Workbook wbook = app.Workbooks.Add(System.Reflection.Missing.Value);        _excel.Worksheet tsheet = (_excel.Worksheet)wbook.ActiveSheet;        //为 tsheet.Cells 创建 Range ,方便释放资源
            _excel.Range rans = (_excel.Range)tsheet.Cells;        //创建ran为了下面赋值时候使用
            _excel.Range ran = null;        for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ran = (_excel.Range)rans[i + 1, j + 1];
                    ran.Value2 = dt.Rows[i][j];
                    NAR(ran);
                    //不要如下方式赋值
                    //tsheet.Cells[i + 1, j + 1] = dt.Rows[i][j];
                }
            }
            NAR(rans);
            NAR(tsheet);
            //保存信息
            wbook.Close(true,newpath, System.Reflection.Missing.Value);
            NAR(wbook);
            NAR(wbooks);
            app.Quit();
            NAR(app);
        }
      

  5.   

    谢谢楼上,不过我试了,
    好象建不建 range ,与释放资源关系不太子。前三个对象,比较建变量,好象 range 不太需要。