Excel.exe未关闭

时间:2016-01-11 03:31:50

标签: c# excel

我在这里很新。我已经搜索了这个论坛,我看到那个双点COM对象不好,我已经将open()改为一个点。但我仍然无法杀死进程。添加了Marshal.ReleaseComObject,它仍然无法正常工作。任何人都可以指出我的代码中的错误?感谢。

               Application excel = new Application();
                try
                {
                    //excel.Workbooks.Open(filePath); //Apparently double dot for COM object is not a good practice
                    var excelworkbooks = excel.Workbooks;
                    var openworkbook = excelworkbooks.Open(filePath);

                    excel.Visible = false;
                    if (excel.Workbooks.Count > 0)
                    {
                        IEnumerator wsEnumerator = excel.ActiveWorkbook.Worksheets.GetEnumerator();
                        object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

                        while (wsEnumerator.MoveNext())
                        {
                            Microsoft.Office.Interop.Excel.Worksheet wsCurrent = (Microsoft.Office.Interop.Excel.Worksheet)wsEnumerator.Current;
                            String outputFile = "OutputFile.html";
                            if(!File.Exists(filePath+outputFile))
                            {
                                wsCurrent.SaveAs(filePath + outputFile, format);
                            }
                            break;
                        }
                    }
                    excel.Workbooks.Close();
                    excel.Application.Quit();
                    excel.Quit();

                    Marshal.ReleaseComObject(excelworkbooks);
                    Marshal.ReleaseComObject(openworkbook);
                }
                finally
                {                        
                    Marshal.ReleaseComObject(excel.Workbooks);
                    Marshal.ReleaseComObject(excel.Application);
                    Marshal.ReleaseComObject(excel);
                }

0 个答案:

没有答案