数据访问后在C#中关闭Excel应用程序 [英] Closing Excel Application Process in C# after Data Access
问题描述
我正在用 C# 编写一个应用程序,它打开一个 Excel 模板文件以进行读/写操作.我想当用户关闭应用程序时,excel 应用程序进程已关闭,而不保存 excel 文件.多次运行应用后,请查看我的任务管理器.
I'm writing an application in C# that opens an Excel template file for read/write operations. I want to when user closes the application, excel application process has been closed, without saving excel file. See my Task Manager after multiple runs of the app.
我用这段代码打开excel文件:
I use this code to open the excel file :
public Excel.Application excelApp = new Excel.Application();
public Excel.Workbook excelBook;
excelBook = excelApp.Workbooks.Add(@"C:/pape.xltx");
对于数据访问,我使用此代码:
and for data access I use this code :
Excel.Worksheet excelSheet = (Worksheet)(excelBook.Worksheets[1]);
excelSheet.DisplayRightToLeft = true;
Range rng;
rng = excelSheet.get_Range("C2");
rng.Value2 = txtName.Text;
我在stackoverflow中看到类似的问题,例如这个问题 和 this,并测试答案,但它不起作用.
I see similar questions in stackoverflow such as this question and this, and test answers, but it doesn't works.
推荐答案
试试这个:
excelBook.Close(0);
excelApp.Quit();
关闭工作簿时,您有三个可选参数:
When closing the work-book, you have three optional parameters:
Workbook.close SaveChanges, filename, routeworkbook
Workbook.Close(false)
或者,如果您正在进行后期绑定,有时使用零会更容易Workbook.Close(0)
这就是我在自动关闭工作簿时所做的.
Workbook.Close(false)
or if you are doing late binding, it sometimes is easier to use zero
Workbook.Close(0)
That is how I've done it when automating closing of workbooks.
我也去查找了它的文档,并在这里找到了它:Excel 工作簿关闭
Also I went and looked up the documentation for it, and found it here: Excel Workbook Close
谢谢,
这篇关于数据访问后在C#中关闭Excel应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!