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

问题描述 投票:1回答:1

在我目前的项目中,我需要打开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();

提前致谢。

c# forms office-interop
1个回答
0
投票

代码似乎是正确的,只需确保将结束内容放在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);
}
© www.soinside.com 2019 - 2024. All rights reserved.