C#程序,用于使用关闭前事件处理程序自动处理excel,而不保存编辑后的excel文件
C# program for automating excel with before close event handler not saving edited excel file
我想避免通过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);
我仍然不确定我最初的代码出了什么问题。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- 如何连接C++和Excel(从C++程序输出到Excel电子表格)
- C 程序要读取包含两个双重值的Excel数据的单列,这些值由逗号分隔并存储在自己的数组中
- C 如何通过同一MFC应用程序通过System Command打开Excel文件后关闭CMD EXE
- 将sheet1(excel)转换为标准sheet2(excel)的程序
- 如何从Qt应用程序驱动Excel窗口
- Excel调用C++程序的函数
- C#程序,用于使用关闭前事件处理程序自动处理excel,而不保存编辑后的excel文件
- 在Windows应用程序中从rdl/rdlc文件中写入Excel文件
- 通过OLE Automation从c++应用程序运行存储在Excel工作簿中的指定宏
- 从xll外接程序返回的较长unicode字符串导致Excel崩溃
- #VAL!尝试使用 DLL 从 Excel 调用简单 C++ 程序时的答案