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

后端 未结 3 1385
深忆病人
深忆病人 2020-12-30 01:46

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

相关标签:
3条回答
  • 2020-12-30 01:49

    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

    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.

    I like checking if the file exists before saving it:

            if (File.Exists(SaveAsName))
            {
                File.Delete(SaveAsName); 
            }
    
    0 讨论(0)
  • 2020-12-30 01:51

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

    ExcelApp.DisplayAlerts = False

    0 讨论(0)
  • 2020-12-30 02:03

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

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

    Hope this helps

    0 讨论(0)
提交回复
热议问题