C#-以编程方式打开和关闭Excel文件的正确方法 [英] C# - Proper way to open and close an excel file programmatically

查看:537
本文介绍了C#-以编程方式打开和关闭Excel文件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到打开和关闭excel文件的正确方法.

I can't seem to find the proper way to open and close an excel file.

这是我打开文件所需要的,我发现它太复杂了:

Here is what I have to open my file, which I find overly complicated:

        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
            0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);
        Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;

        Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.Worksheets[2];
        sheet.Select(Type.Missing);

我不知道如何正确关闭它.我需要使用相同的路径保存它,并确保在关闭后excel仍未在后台运行.

I have no idea how to properly close it. I need to save it with the same path and make sure excel is not still running in the background after it is closed.

有人可以帮我轻松吗? 谢谢

Can someone make it easy for me? Thanks

推荐答案

半伪代码:

using Excel = Microsoft.Office.Interop.Excel;

# declare the application object
Excel.Application xl = new Excel.Application();

# open a file
Excel.Workbook wb = xl.Workbooks.Open("some_file.xlsx");

# do stuff ....

# close the file
wb.Close();

# close the application and release resources
xl.Quit();

这篇关于C#-以编程方式打开和关闭Excel文件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆