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

后端 未结 3 1384
深忆病人
深忆病人 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); 
            }
    

提交回复
热议问题