Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用Winform应用程序缓存_C#_Excel_Winforms_Office Interop_Excel Interop - Fatal编程技术网

C# 禁用Winform应用程序缓存

C# 禁用Winform应用程序缓存,c#,excel,winforms,office-interop,excel-interop,C#,Excel,Winforms,Office Interop,Excel Interop,我开发了winform应用程序,可以从excel中读取数据并将其转换为文本文件。 为了使用Excel,我使用了Microsoft.Office.Interop.Excel库 这是我的密码: private Excel.Application excel = null; private Excel.Sheets sheets = null; private Excel.Workbook excelWorkbook = null; private Ex

我开发了winform应用程序,可以从excel中读取数据并将其转换为文本文件。 为了使用Excel,我使用了Microsoft.Office.Interop.Excel库

这是我的密码:

private Excel.Application excel = null;
        private Excel.Sheets sheets = null;
        private Excel.Workbook excelWorkbook = null;
        private Excel.Workbooks excelWorkbooks = null;
        private Excel._Worksheet worksheet = null;
        private Excel.Range usedRange = null;    

        public ExcelFacade() { }

        public ExcelFacade(string fileName)
        {
            excel = new Excel.Application(); 

            excelWorkbooks = excel.Workbooks;
            excelWorkbook = excelWorkbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
            sheets = excelWorkbook.Sheets;
        }
使用Excel完成工作后,我调用下一个方法(从):

但是,如果我更改excel并重新运行我的应用程序,则应用程序不会使用更新的excel,它看起来像是从旧excel读取的。这种行为看起来像缓存,我不知道如何禁用它

如果我更改代码中的某些内容(例如空白)并重新构建应用程序,它将运行良好并获得正确的Excel文件,这一事实进一步加强了上述内容。
有什么建议吗?

正如@vbnet3d所说,使用ClosedXML库可以解决所有问题

我没有太多地使用interop,所以我不确定是否可以直接回答您的问题-但是您是否可以使用ClosedXML这样的库来代替?或者,如果这是XLS文件,您可以使用OLE吗?这两种方法都可以完全避免你的问题。
 public void Dispose()
        {


            foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets)
            {
                while (Marshal.ReleaseComObject(sheet) != 0) { }
            }

            excelWorkbook.Close();
            excelWorkbooks.Close();
            excel.Quit();

            var chromeDriverProcesses = Process.GetProcesses().
                         Where(pr => pr.ProcessName.ToLower().Contains("excel"));
            foreach (var process in chromeDriverProcesses)
            {
                process.Kill();
            }


            //while (Marshal.ReleaseComObject(usedRange) != 0) { }
            //while (Marshal.ReleaseComObject(worksheet) != 0) { }
            while (Marshal.ReleaseComObject(sheets) != 0) { }
            while (Marshal.ReleaseComObject(excelWorkbook) != 0) { }
            while (Marshal.ReleaseComObject(excelWorkbooks) != 0) { }
            //while (Marshal.ReleaseComObject(excel.Application) != 0)
            //{ }
            while (Marshal.ReleaseComObject(excel) != 0) { }         
            usedRange = null;
            worksheet = null;
            excelWorkbook = null;
            excelWorkbooks = null;
            sheets = null;
            excel = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

        }
    }