简体   繁体   English

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

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

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. 有没有办法通过excel互操作保存对excel电子表格的更改(在这种情况下,我正在向其添加工作表),而不会提示用户是否要用更改覆盖现有文件。 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. 我不希望用户甚至在我的应用程序中看到电子表格打开,因此弹出一个消息框询问他们是否要覆盖该文件似乎非常不合适并且可能使用户感到困惑。

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. 下面是我用来关闭/保存excel文件的代码。 The workbook.close() method line is the one that is reportedly throwing the unhandled exception. workbook.close()方法行是据报道抛出未处理异常的行。

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

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 希望这可以帮助

Only this code will Require for stop override alert or Template already in use 只有此代码才会要求停止覆盖警报或模板已在使用中

ExcelApp.DisplayAlerts = False ExcelApp.DisplayAlerts = False

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. 设置此标志将在excel文件上设置此属性,而不仅仅是在程序中。 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. 这意味着如果用户对文件进行了更改并将其关闭(通过单击X),则不会提示他们保存文件,以后会引起挫败感。 It will also disable any other prompts excel would typically post. 它还将禁用excel通常发布的任何其他提示。

I like checking if the file exists before saving it: 我喜欢在保存之前检查文件是否存在:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM