如何使用Excel Interop - c#保存/覆盖现有Excel文件

时间:2021-11-26 07:36:48

Is there a way to save changes to an excel spreadsheet through the excel interop (in this case I am adding a worksheet to it) without having it prompt the user if they want to overwrite the existing file with the changes. I do not want the user to even see the spreadsheet open in my application so having a message box popping up asking them if they want to overwrite the file seems very out of place and possibly confusing to the user.

是否有一种方法可以通过excel interop将更改保存到excel电子表格中(在本例中,我添加了一个工作表),如果用户想要用更改覆盖现有的文件,就不会提示用户。我不希望用户看到我的应用程序中打开的电子表格,所以弹出一个消息框询问他们是否想要覆盖文件,这看起来非常不合适,用户可能会感到困惑。

I am using the workbook.SaveAs(fileloaction) method.

我正在使用workbook.SaveAs(fileloaction)方法。

Here is where I am initializing the COM reference objects for the excel interop.

在这里,我正在初始化用于excel互操作的COM引用对象。

private Excel.Application app = null;
    private Excel.Workbook workbook = null;

    public Excel.Workbook Workbook
    {
        get { return workbook; }
        set { workbook = value; }
    }
    private Excel.Worksheet worksheet = null;
    private Excel.Range workSheet_range = null;

Below is the code I am using to close/save the excel file. The workbook.close() method line is the one that is reportedly throwing the unhandled exception.

下面是我用来关闭/保存excel文件的代码。关闭()方法行是报告抛出未处理异常的方法。

 workbook.Close(true, startForm.excelFileLocation, Missing.Value);
            Marshal.ReleaseComObject(app);
            app = null;
            System.GC.Collect();

3 个解决方案

#1


52  

Basically, all you need is ExcelApp.DisplayAlerts = False - Here's how I do it, though:

基本上,你所需要的就是ExcelApp。DisplayAlerts = False -以下是我的做法:

ExcelApp.DisplayAlerts = False
ExcelWorkbook.Close(SaveChanges:=True, Filename:=CurDir & FileToSave)

Hope this helps

希望这有助于

#2


8  

Only this code will Require for stop override alert or Template already in use

只有此代码将需要停止覆盖警告或已在使用的模板。

ExcelApp.DisplayAlerts = False

ExcelApp。DisplayAlerts = False

#3


0  

I know this is an old post, but I wanted to share a way to make this work without causing possible frustration in the future.

我知道这是一个古老的帖子,但我想分享一个方法,使这项工作,而不会引起未来可能的挫折。

First what I do not like about using: ExcelApp.DisplayAlerts = False

首先,我不喜欢使用ExcelApp。DisplayAlerts = False

Setting this flag will set this property on the excel file, not just in your program. This means that if a user makes changes to the file and closes it (by clicking the X), they will not be prompted to save the file and will cause frustration later. It will also disable any other prompts excel would typically post.

设置此标志将在excel文件中设置此属性,而不仅仅是在程序中。这意味着,如果用户对该文件进行更改并关闭它(通过单击X),则不会提示他们保存该文件,并将导致稍后的挫折。它还将禁用excel通常发布的任何其他提示符。

I like checking if the file exists before saving it:

我喜欢在保存之前检查文件是否存在:

        if (File.Exists(SaveAsName))
        {
            File.Delete(SaveAsName); 
        }

#1


52  

Basically, all you need is ExcelApp.DisplayAlerts = False - Here's how I do it, though:

基本上,你所需要的就是ExcelApp。DisplayAlerts = False -以下是我的做法:

ExcelApp.DisplayAlerts = False
ExcelWorkbook.Close(SaveChanges:=True, Filename:=CurDir & FileToSave)

Hope this helps

希望这有助于

#2


8  

Only this code will Require for stop override alert or Template already in use

只有此代码将需要停止覆盖警告或已在使用的模板。

ExcelApp.DisplayAlerts = False

ExcelApp。DisplayAlerts = False

#3


0  

I know this is an old post, but I wanted to share a way to make this work without causing possible frustration in the future.

我知道这是一个古老的帖子,但我想分享一个方法,使这项工作,而不会引起未来可能的挫折。

First what I do not like about using: ExcelApp.DisplayAlerts = False

首先,我不喜欢使用ExcelApp。DisplayAlerts = False

Setting this flag will set this property on the excel file, not just in your program. This means that if a user makes changes to the file and closes it (by clicking the X), they will not be prompted to save the file and will cause frustration later. It will also disable any other prompts excel would typically post.

设置此标志将在excel文件中设置此属性,而不仅仅是在程序中。这意味着,如果用户对该文件进行更改并关闭它(通过单击X),则不会提示他们保存该文件,并将导致稍后的挫折。它还将禁用excel通常发布的任何其他提示符。

I like checking if the file exists before saving it:

我喜欢在保存之前检查文件是否存在:

        if (File.Exists(SaveAsName))
        {
            File.Delete(SaveAsName); 
        }