C#程序,用于使用关闭前事件处理程序自动处理excel,而不保存编辑后的excel文件

C# program for automating excel with before close event handler not saving edited excel file

本文关键字:excel 程序 保存 文件 处理 编辑 用于 事件处理      更新时间:2023-10-16

我想避免通过c++代码调用Excel ole来启动Excel。我还想等excel关闭后再继续。为此,我实现了下面的C#程序,并使用系统调用来执行它。它很有效,但我对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;
        }
    }
}

我发现了这个问题。中有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);

我把它改成了following。

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);

我仍然不确定我最初的代码出了什么问题。