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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13055369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:29:58  来源:igfitidea点击:

How to Save/Overwrite existing Excel file with Excel Interop - C#

c#excelexcel-interop

提问by user1546315

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 互操作将更改保存到 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() 方法行是据报道抛出未处理异常的行。

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

采纳答案by John Bustos

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

希望这可以帮助

回答by Shivam Srivastava

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

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

ExcelApp.DisplayAlerts = False

ExcelApp.DisplayAlerts = False

回答by user5581710

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