110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Excel.Application eApp;
Excel.Workbooks oBooks;
Excel.Workbook oBook;
Excel.Worksheet oSheet;
public Form1()
{
InitializeComponent();
axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(axWebBrowser1_NavigateComplete2);
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
object refmissing = DBNull.Value;
string path = @"E:\xp.xls";
axWebBrowser1.Navigate(path, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
NAR(oSheet);
if (oBook != null)
oBook.Close(false);
NAR(oBook);
NAR(oBooks);
if (eApp != null)
eApp.Quit();
NAR(eApp);
axWebBrowser1.Dispose();
}
void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
//Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);
//由于打开的是excel文件,所以这里的oApplication 其实就是Excel.Application
eApp = (Excel.Application)oApplication;//这样就可以象上文中所述来操作Excel了。
eApp.Visible = false;
}
private void NAR(Object o)
{
if (o != null && o != DBNull.Value)
{
try { System.Runtime.InteropServices.Marshal.ReleaseComObject(o); }
catch { }
finally { o = null; }
}
}
}
}
axWebBrowser1.Navigate(path, ref refmissing, ref refmissing, ref refmissing, ref refmissing);