在c#线程中正确closuresMicrosoft.Office.Interop.Excel
我做了一个应用程序,将数据logging到一个excel文件。 在closures应用程序之前,我无法打开excel文件。 在任务pipe理器中,excel.exe在我closures了代码中的excel应用程序后仍然在运行。 我已经尝试了至less11种方法来closuresexcel,但它不起作用。
所有的代码都是从一个线程执行的:
thread = new Thread(()=> export_parameters(path));
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); } }
编辑:这个问题是不同的数据访问后closuresC#中的Excel应用程序进程,因为我从一个线程,而不是一个方法执行。 提供的答案不起作用。 我自己解决了这个问题:
private Microsoft.Office.Interop.Excel._Application excel;//excel application that is used for creating excel workbooks(files) private Microsoft.Office.Interop.Excel._Workbook workbook;//excel workbook(file) private Microsoft.Office.Interop.Excel._Worksheet worksheet; excel = new Microsoft.Office.Interop.Excel.Application(); workbook = excel.Workbooks.Add(); worksheet = workbook.ActiveSheet; workbook.Close(); excel.Quit(); GC.Collect(); GC.WaitForPendingFinalizers(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel); worksheet = null; workbook = null; excel = null;