简体   繁体   English

如何在Winform启动时打开Excel工作簿并在退出时关闭

[英]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. 在我当前的项目中,我需要打开2张excel工作簿,一本可以阅读,一本可以写。 Whats the best way to open these workbooks on the start of my winform app so i can easily access them later? 在winform应用程序启动时打开这些工作簿的最佳方法是什么,以便以后可以轻松访问它们? 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. 该代码似乎正确,只需确保将最后的内容放到finally以便即使在弹出异常时也可以释放资源,否则您将无限期地陷入一个Excel进程中,该进程将保留打开的文件锁,直到您在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: 此外,我明确地设置了Excel应用程序被隐藏,避免打草惊蛇弹出窗口和也接近并释放Workbooks对象(来自不同Workbook从Excel的应用程序):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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