简体   繁体   中英

How to open excel workbook on start of winform and close on exit

in my current project i need to open 2 excel workbooks, one to read from and one to write to. Whats the best way to open these workbooks on the start of my winform app so i can easily access them later? And then how do i close both workbooks efficiently so that no background processes hang around?

here is how i open it currently, are there any better ways to do this?

    // Get the Excel application object.
            Excel.Application excel_app = new Excel.Application();


            string path = @"Here goes the read excel path";
            string path2 = @"Here goes the write excel path";
            // Open the workbook.


            Excel.Workbook workbook = excel_app.Workbooks.Open(path
                ,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);

            Excel.Workbook workbook2 = excel_app.Workbooks.Open(path2
            ,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);


            FindSheet sheetFind = new FindSheet();
            FindSheet2 sheetFind2 = new FindSheet2();
            // See if the worksheet already exists.

            string sheet_name = DateTime.Now.ToString("MM-dd-yy");
            string sheet_name2 = "Blad1";


            Worksheet sheet = sheetFind.Findsheet(workbook, sheet_name);
            Worksheet sheet2 = sheetFind2.Findsheet2(workbook2, 

And this is how i close everything currently, any improvements?

 workbook2.Close(true, Type.Missing, Type.Missing);
            workbook.Close(true, Type.Missing, Type.Missing);
            excel_app.Quit();

            //release all memory - stop EXCEL.exe from hanging around.
            if (workbook != null) { Marshal.ReleaseComObject(workbook); } //release each workbook like this
            if (sheet != null) { Marshal.ReleaseComObject(sheet); } //release each worksheet like this
            if (excel_app != null) { Marshal.ReleaseComObject(excel_app); } //release the Excel application
            if (workbook2 != null) { Marshal.ReleaseComObject(workbook2); } 
            if (sheet2 != null) { Marshal.ReleaseComObject(sheet2); } 

            workbook = null; //set each memory reference to null.
            workbook2 = null;
            sheet = null;
            sheet2 = null;
            excel_app= null;
            GC.Collect();

Thanks in advance.

The code seems correct, just make sure to put the closing stuff on a finally so the resources are released even when an exception pops up, otherwise you will be stuck with an Excel process indefinitely that retains opened files locks until you kill it manually on Windows.

Additionally I explicitly set the excel app to be hidden, avoid alert pop ups and also close and release the Workbooks object (different from the from Workbook ) from the Excel App:

Application excel = null;
Workbooks workbooks = null;
Workbook workbook = null;

try
{
    excel = new Application();
    excel.Visible = false;
    excel.DisplayAlerts = false;

    workbooks = excel.Workbooks;
    workbook = workbooks.YourSearchOrAddWorkbookMethod();

    /*Your operations here*/
}
finally
{
    if (workbook != null)
        workbook.Close();

    if (workbooks != null)
        workbooks.Close();

    if (excel != null)
        excel.Quit();

    if (workbook != null)
        Marshal.ReleaseComObject(workbook);

    if (workbooks != null)
        Marshal.ReleaseComObject(workbooks);

    if (excel != null)
        Marshal.ReleaseComObject(excel);
}

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