2014-10-29 140 views
0

我想通过我的C++代码避免Excel ole调用来启动excel。我还想在继续之前等待excel关闭。为此,我实现了下面的C#程序,并使用系统调用来执行它。它可以工作,但是我对excel所做的任何更改都没有得到保存。我不知道发生了什么。 你可以请帮忙。C#程序自动执行excel与关闭事件处理程序不保存已编辑的excel文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

namespace excelHandler 
{ 
    class Program 
    { 
     private static bool isClosed = false; 

     private static void app_WorkbookBeforeClose(Excel.Workbook wb, ref bool cancel) 
     { 
      if (!wb.Saved) 
      { 
       DialogResult result = MessageBox.Show("Do you want to save the " + 
        "changes you made to " + wb.FullName + "?", "Blizzard Excel", 
        MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning, 
        MessageBoxDefaultButton.Button1, 
        MessageBoxOptions.DefaultDesktopOnly); 

       switch (result) 
       { 
        case DialogResult.Yes: 
         wb.Save(); 
         break; 

        case DialogResult.Cancel: 
         cancel = true; 
         break; 

         // The following code ensures that the default Save File 
         // dialog is not displayed. 
        case DialogResult.No: 
         wb.Saved = true; 
         break; 
       } 
      } 

      isClosed = !cancel; 
      // wb.Parent 
     } 

     static int Main(string[] args) 
     { 
      int returnValue = 0; 

      if (args.Length != 1) 
      { 
       Console.WriteLine("-E- excelHandler takes single XLXS as input"); 
       return -1; 
      } 
      string inputFile = args[0]; 

      if (!File.Exists(inputFile)) 
      { 
       Console.WriteLine("-E- Input File doesn't exist: {0}", inputFile); 
       return -1; 
      } 

      Excel.Application xlApp = new Excel.Application(); 
      try 
      { 
       Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(inputFile); 
       xlApp.Visible = true; 

       Excel.AppEvents_WorkbookBeforeCloseEventHandler Event_BeforeBookClose; 

       Event_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(app_WorkbookBeforeClose); 
       xlApp.WorkbookBeforeClose += Event_BeforeBookClose; 

       // || 
       while (!isClosed) 
       { 
        System.Threading.Thread.Sleep(300); 
       } 

       //xlWorkBook.Close(true); 
       Marshal.ReleaseComObject(xlWorkBook); 
      } 
      catch 
      { 
      } 
      finally 
      { 
       xlApp.Quit(); 
       Marshal.ReleaseComObject(xlApp); 
      } 
      return returnValue; 
     } 
    } 
} 
+0

我进一步调试它。看起来像我的messageBox没有注册正确的按钮。 – Nihar 2014-10-30 17:45:37

回答

0

我发现了这个问题。它与MessageBoxOptions.DefaultDesktopOnly选项

DialogResult result = MessageBox.Show("Do you want to save the " + 
       "changes you made to " + wb.FullName + "?", "Blizzard Excel", 
       MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning, 
       MessageBoxDefaultButton.Button1, 
       MessageBoxOptions.DefaultDesktopOnly); 

我改变它到以下让它工作。

NativeWindow nw = new System.Windows.Forms.NativeWindow(); 
      nw.AssignHandle(new IntPtr(wb.Parent.Hwnd)); 

DialogResult result = MessageBox.Show(nw,"Do you want to save the " + 
      "changes you made to " + wb.FullName + "?", "Blizzard Excel", 
      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, 
      MessageBoxDefaultButton.Button1); 

我仍然不确定我的初始代码有什么问题。