2014-10-29 68 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); 

我仍然不確定我的初始代碼有什麼問題。

相關問題