Closing an Excel Workbook

前端 未结 5 749
無奈伤痛
無奈伤痛 2021-01-13 00:34

I have a bit of code that opens an xls workbook;

Excel.Workbooks workBooks;
workBooks = excelApp.Workbooks;
workbook = workBooks.Open(sourceFilePath + source         


        
相关标签:
5条回答
  • 2021-01-13 00:52

    Have you considered the fact that the system might still be in the process of saving the file when you attempt to close it? I'm just saying, to be sure add a delay(Thread.Sleep(1000) in C# for example) before the close to see if this is the problem.

    0 讨论(0)
  • 2021-01-13 00:57

    Why not combine the 2. This will take care of any problems with closing before saving is complete. There is an option in the Close method to save the file.

    workbook.Close(true, fileName, Missing.Value);
    

    Also if the file is saving correctly, and your problem is purely because the excel.exe process is still running, it could be because you didn't close and release EVERYTHING needed. I have had this before and developed a more complete close down routine. My code for shutting down an excel file is:

            book.Close(true, fileName, Missing.Value); //close and save individual book
            allBooks.Close(); //close all books
            excel.Quit();
            Marshal.ReleaseComObject(allCells); //any used range objects
            Marshal.ReleaseComObject(sheet);
            Marshal.ReleaseComObject(sheets);
            Marshal.ReleaseComObject(book);
            Marshal.ReleaseComObject(allBooks);
            Marshal.ReleaseComObject(excel);
    

    This works 100% of the time for me.

    0 讨论(0)
  • 2021-01-13 01:03
        EXCEL.Application excel = new EXCEL.Application();
        try
        {
            EXCEL.Workbook book = excel.Application.Workbooks.Open(path);
            EXCEL.Worksheet sheet = book.Worksheets[1];
            // yout operation
    
        }
        catch (Exception ex) { MessageBox.Show("readExcel:" + ex.Message); }
        finally
        {
            KillExcel(excel);
            System.Threading.Thread.Sleep(100);
        }
    
    
    [DllImport("User32.dll")]
    public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);
    private static void KillExcel(EXCEL.Application theApp)
    {
        int id = 0;
        IntPtr intptr = new IntPtr(theApp.Hwnd);
        System.Diagnostics.Process p = null;
        try
        {
            GetWindowThreadProcessId(intptr, out id);
            p = System.Diagnostics.Process.GetProcessById(id);
            if (p != null)
            {
                p.Kill();
                p.Dispose();
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("KillExcel:" + ex.Message);
        }
    }
    

    Thank you!!!!

    0 讨论(0)
  • 2021-01-13 01:04

    Here is the solution

    first: using EXCEL = Microsoft.Office.Interop.Excel;

    and then, path is where your excel locates.

            EXCEL.Application excel = new EXCEL.Application();
            try
            {
                EXCEL.Workbook book = excel.Application.Workbooks.Open(path);
                EXCEL.Worksheet sheet = book.Worksheets[1];
                // yout operation
    
            }
            catch (Exception ex) { MessageBox.Show("readExcel:" + ex.Message); }
            finally
            {
                KillExcel(excel);
                System.Threading.Thread.Sleep(100);
            }
    
    
    
        [DllImport("User32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);
        private static void KillExcel(EXCEL.Application theApp)
        {
            int id = 0;
            IntPtr intptr = new IntPtr(theApp.Hwnd);
            System.Diagnostics.Process p = null;
            try
            {
                GetWindowThreadProcessId(intptr, out id);
                p = System.Diagnostics.Process.GetProcessById(id);
                if (p != null)
                {
                    p.Kill();
                    p.Dispose();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("KillExcel:" + ex.Message);
            }
        }
    
    0 讨论(0)
  • 2021-01-13 01:16

    This question keeps popping up see:

    How to properly clean up Excel interop objects in C#

    You need to call System.Runtime.InteropServices.Marshal.ReleaseComObject() on every excel object you use, even invisible ones, e.g.:

    var worksheet = excelApp.Worksheets.Open()

    There are two objects here: 1. The obvious 'Worksheet' opened with Open() 2. The "invisible" collection 'Worksheets'.

    Both of them need to be released (so you better keep a reference for Worksheets):

    var wkCol = excelApp.Worksheets;
    var worksheet = wkCol.Open();
    
    0 讨论(0)
提交回复
热议问题