在c#thread中正确关闭Microsoft.Office.Interop.Excel

时间:2016-12-09 17:25:38

标签: c# excel

我已经创建了一个将数据记录到excel文件的应用程序。 在我关闭应用程序之前,我无法打开excel文件。 在任务管理器中,在我在代码中关闭excel应用程序后,excel.exe仍在运行。 我已经尝试过至少11种关闭excel的方法,但它不起作用。

所有代码都是从一个线程执行的:

thread = new Thread(()=> export_parameters(path));

.myClass{
  margin-left: auto;
  margin-right:auto;
  text-align: center;
}

编辑:这个问题与Closing Excel Application Process in C# after Data Access不同,因为我是从一个线程执行的,而不是一个方法。提供的答案不起作用。 我自己解决了这个问题:

    public class xlsx_logfile
    {
        private Microsoft.Office.Interop.Excel._Application excel;//excel application that is used for creating excel workbooks(files)
        private Microsoft.Office.Interop.Excel.Workbooks workbooks;
        private Microsoft.Office.Interop.Excel._Workbook workbook;//excel workbook(file)
        private Microsoft.Office.Interop.Excel._Worksheet worksheet;

        private string filename;

        public xlsx_logfile(string path)
        {
            //destructor?

            excel = new Microsoft.Office.Interop.Excel.Application();
            workbooks = excel.Workbooks; 
            workbook = workbooks.Add(Type.Missing);
            worksheet = null;

            worksheet = workbook.ActiveSheet;
            worksheet.Name = "sensordata";

            filename = path;


        }

        public void column_add(uint column_zerobased_index,string header, string format, double[] data)
        {
            Microsoft.Office.Interop.Excel.Range formatRange;

            Microsoft.Office.Interop.Excel.Range c1;
            //Microsoft.Office.Interop.Excel.Range c2;

            column_zerobased_index = 0;

            //var worksheet = workbook.ActiveSheet;



            c1 = worksheet.Cells[1, column_zerobased_index+1];
            //c2 = oSheet.Cells[4, 4];

            formatRange = worksheet.get_Range(c1,c1);//column_zerobased_index + 1
            formatRange.EntireColumn.NumberFormat = "0.0";

            worksheet.Cells[1, column_zerobased_index+1].NumberFormat = "@";
            worksheet.Cells[1, column_zerobased_index + 1] = header;
            //todo use: https://stackoverflow.com/questions/3989122/microsoft-office-interop-excel-really-slow
            for (int i = 0; i < data.Length; ++i)
            {
                worksheet.Cells[i + 1 + 1, column_zerobased_index+1] = data[i];
            }

            // Cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(formatRange);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(c1);
        }

        void close(string filename)
        {
            //worksheet = null;
            //workbook.Close();
            //workbook = null;
            //excel = null;


            //excel.Quit();
            //worksheet = null;
            //workbook = null;
            //excel = null;


            //worksheet = null;
            //workbook.Close();
            //workbook = null;
            //excel.Quit();
            //excel = null;


            //worksheet = null;
            //workbook.Close();
            //workbook = null;


            //var workbooks_local = excel.Workbooks;
            //var workbook_local = workbooks_local.Open(filename);

            //workbook_local.Close();
            //workbooks_local.Close();
            //excel.Quit();

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook_local);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks_local);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);


            //System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            //worksheet = null;
            ////workbook.Close();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            //workbook = null;
            ////excel.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
            //excel = null;


            ////based on https://stackoverflow.com/questions/1526685/c-how-can-i-open-and-close-an-excel-workbook
            //workbook.Close(false, filename, null);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            //excel.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
            //workbook = null;
            //excel = null;
            //GC.Collect();
            //GC.WaitForPendingFinalizers();
            //GC.Collect();
            //GC.WaitForPendingFinalizers();

            //var aap = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            //worksheet = null;
            //workbook.Close();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            //workbook = null;
            //workbooks.Close();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);                
            //workbooks = null;
            //excel.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
            //excel = null;

            // Cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRng);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(worksheet);

            workbook.Close(Type.Missing, Type.Missing, Type.Missing);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);

            excel.Quit();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel);

        }

        public void save()
        {
            workbook.SaveAs(filename);
            close(filename);
        }

    }

0 个答案:

没有答案