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

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

在我当前的项目中,我需要打开 2 excel 个工作簿,一个用于读取,一个用于写入。在我的 winform 应用程序启动时打开这些工作簿的最佳方式是什么,以便我以后可以轻松访问它们?然后我如何有效地关闭两个工作簿以便没有后台进程挂起?

这是我目前打开它的方式,还有更好的方法吗?

    // 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, 

这就是我目前关闭所有内容的方式,有什么改进吗?

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

提前致谢。

代码看起来是正确的,只是确保将关闭的东西放在finally上,这样即使弹出异常也会释放资源,否则你会无限期地陷入 Excel 进程,该进程保留打开的文件锁,直到您在 Windows.

上手动终止它

此外,我明确将 excel 应用程序设置为隐藏,避免弹出警报和 也从 Excel App:

关闭并释放 Workbooks 对象(不同于来自 Workbook 的对象)
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);
}