首页 > 代码库 > C#打开Excel并响应客户端自动化事件

C#打开Excel并响应客户端自动化事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MSExcel = Microsoft.Office.Interop.Excel;

namespace word
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MSExcel.Application excelApp;
MSExcel.Workbook book;


private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog P_GetFile = new OpenFileDialog();
DialogResult P_dr = P_GetFile.ShowDialog();
object obj = (object)(P_GetFile.FileName);
if (P_dr == DialogResult.OK)
{
excelApp = new MSExcel.Application();
book = excelApp.Application.Workbooks.Add(obj);
excelApp.Visible = true;
}
excelApp.WorkbookBeforeSave += new MSExcel.AppEvents_WorkbookBeforeSaveEventHandler(excelApp_WorkbookBeforeSave);
}
private void excelApp_WorkbookBeforeSave(MSExcel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
MessageBox.Show("hello Excel");
}
}
}

C#打开Excel并响应客户端自动化事件