为什么我会看到两个不同的SaveAs Overwrite提示?

时间:2016-10-31 11:39:44

标签: c# excel

我有两个不同的Excel文件,并对两者进行一些更改。当我保存为...时,会提示每个文件确认现有文件以及覆盖它的选项。我知道我可以完全避免使用Save()命令,但我试图理解为什么有两个不同的外观覆盖提示?

一切都妥善保存,不会发生错误。

在我的C#代码中,我使用的是MS Excel 14.0对象库版本。 (Excel 2010)
我使用的IDE是MS Visual Studio 2015社区 操作系统是Win 7 x64。硬件风味AMD。

感谢您的时间。

SaveAs对话1:

enter image description here

SaveAs对话框2

SaveAs Dialog 2

我用来测试这个代码...

using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
namespace SO_Question
{
   public partial class Form1 : Form
   {
      public Form1()
      {
        InitializeComponent();
        EditSaveExcelFiles();
      }

      public static void EditSaveExcelFiles()
      {
         object misValue = System.Reflection.Missing.Value;
         string file1 = @"C:\Users\John\Documents\New Folder\MyExcel.xlsx";
         string file2 = @"C:\Users\John\Documents\New Folder\MyExcel2.xlsx";
         Excel.Application ExcelApp = new Excel.Application();
         Workbook workBook1 = ExcelApp.Workbooks.Open(file1, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
         Workbook workBook2 = ExcelApp.Workbooks.Open(file2, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
         Worksheet workSheet1 = (Worksheet)workBook1.Worksheets.get_Item("Sheet1");
         Worksheet workSheet2 = (Worksheet)workBook2.Worksheets.get_Item("Sheet1");

         // do some stuff
         workSheet1.Cells[1, 1] = "Grazer14";
         workSheet2.Cells[1, 1] = "Grazer24";

         // Different overwrite dialogs?
         try
         {
           workBook1.SaveAs(file1, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
           workBook2.SaveAs(file2, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
         }
         catch (Exception e)
         {
           //when user clicks "No" or "Cancel"
           //MessageBox.Show("No or Cancel: " + e.StackTrace);
         }
         workBook2.Close(true, misValue, misValue);
         workBook1.Close(true, misValue, misValue);
         ExcelApp.Quit();
         System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook1);
         System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook2);
         System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
      }
   }
}

1 个答案:

答案 0 :(得分:1)

您在此处看到的是Excel显示的第一个消息框不是主题,而第二个消息框是。它与您正在保存的特定文件无关(事实上,您可以将同一文件保存两次并仍然看到相同的行为)。

修复方法是在保存之前使Excel应用程序可见:

excelApp.Visible = true;

这样,Excel UI将被正确初始化。您根本无法期望隐形应用程序正确初始化其用户界面 - Excel旨在使用完整的UI或​​根本不使用UI。

如果要避免提示并自动覆盖现有文件,则必须禁用显示警报:

excelApp.DisplayAlerts = false;
workBook1.SaveAs(file1);
excelApp.DisplayAlerts = true;
相关问题