简体   繁体   中英

Disable Winform application cache

I developed winform application that read from excel and transform it to text files. I used Microsoft.Office.Interop.Excel library in order to work with Excel.

Here is my code:

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;
        }

After I finished work with Excel I call next method (from here ):

 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();

        }
    }

However, if I change excel and rerun my application, the updated excel is not taken by application and it looks like it read from old excel. Such behavior is look like caching and I have no idea how to disable it.

The aforementioned is strengthened by the fact that if I change something in my code, eg white space, and rebuild the application, it works brilliant and take right Excel file. Any suggestions?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM