WinForm中使⽤Excel控件
最近项⽬中要在WinForm中使⽤Excel控件,经过⼏天的研究,现在总结⼀下成果。
在WinForm中使⽤Excel控件主要有三种⽅法:WebBrowser、DSOFramer、OWC。下⾯分别描述⼀下如何使⽤。
⼀、WebBrowser
/// 1、添加控件:选择COM选项卡中的Microsoft Web Browser
/// 2、使⽤控件:axWebBrowser1.Navigate(fileNme)
/// 3、添加⼯具条:在axWebBrowser1_NavigateComplete2事件中添加,否则出错。
/// this.axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
/// 4、获取对象:在axWebBrowser1_NavigateComplete2事件中获取。
/
// eDocument = e.pDisp.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, e.pDisp, null);
/// eApplication = (Excel.Application)eDocument.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, eDocument, null);
/// eWorkbook = eApplication.ActiveWorkbook;
/// 5、保存⽂件:eWorkbook.Save();
/// 6、释放⽂件:释放COM对象引⽤
/// Marshal.ReleaseComObject(eWorkbook);
/// Marshal.ReleaseComObject(eApplication);
/// Marshal.ReleaseComObject(eDocument);
后来发现,可以使⽤.NET的webBrowser控件,⽽不⽤添加COM选项卡中的Microsoft Web Browser。
只是获取eApplication 对象⽅式不同,有两种⽅法获取Application对象。
第⼀种其实和axWebBrowser⼀样
代码
private void LoadByActiveXInstance()
{
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)this.webBrowser1.ActiveXInstance;
eDocument = wb.Document;
eApplication = (Excel.Application)eDocument.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, eDocument, null);
//添加⼯具条
eWorkbook = eApplication.ActiveWorkbook; wb.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER); }
第⼆种是使⽤COM⽅法获取,代码⽐较复杂
代码
private Office.CommandBar m_StandardCommandBar = null;
///<summary>
///获取Application⽅式⼆
///添加⼯具条⽅式⼆
///</summary>
private void LoadByAPI()
{
// Creation of the workbook object
if ((eWorkbook = RetrieveWorkbook(FileName)) == null) return;
/
/ Create the Excel.Application
eApplication = eWorkbook.Application;
// Creation of the standard toolbar
m_StandardCommandBar = eApplication.CommandBars["Standard"];
m_StandardCommandBar.Position = Office.MsoBarPosition.msoBarTop;
m_StandardCommandBar.Visible = true;
//foreach (Office.CommandBar bar in eApplication.CommandBars)
// Enable the OpenFile and New buttons
foreach (Office.CommandBarControl control in m_StandardCommandBar.Controls)
{
string name = _accName(Missing.Value);
if (name.Equals("Open")) ((Office.CommandBarButton)control).Enabled = false;
if (name.Equals("Save")) ((Office.CommandBarButton)control).Enabled = false;
}
}
///此⽅法为COM提供的⽅法。可google:COM原理及应⽤命名和绑定技术
///另所有 OLE api 和接⼝的⽬的,参见:support.microsoft/kb/126157/zh-cn
[DllImport("ole32.dll")]
static extern int GetRunningObjectTable(uint reserved, out IRunningObjectTable pprot);
[DllImport("ole32.dll")]
static extern int CreateBindCtx(uint reserved, out IBindCtx pctx);
public Excel.Workbook RetrieveWorkbook(string xlfile)
{
IRunningObjectTable prot = null;
IEnumMoniker pmonkenum = null;
try
{
IntPtr pfetched = IntPtr.Zero;
// Query the running object table (ROT)
if (GetRunningObjectTable(0, out prot) != 0 || prot == null) return null;
prot.EnumRunning(out pmonkenum);
pmonkenum.Reset();
IMoniker[] monikers = new IMoniker[1];
while (pmonkenum.Next(1, monikers, pfetched) == 0)
{
IBindCtx pctx;
控件的使用string filepathname;
CreateBindCtx(0, out pctx);
// Get the name of the file
monikers[0].GetDisplayName(pctx, null, out filepathname);
// Clean up
Marshal.ReleaseComObject(pctx);
// Search for the workbook
// filepathname = @"file:///D:/fly/Book1.xls"
/
/ xlfile = @"D:\fly\Book1.xls"
if (filepathname.IndexOf(xlfile) != -1)
{
object roval;
// Get a handle on the workbook
prot.GetObject(monikers[0], out roval);
return roval as Excel.Workbook;
}
}
}
finally
{
// Clean up
if (prot != null) Marshal.ReleaseComObject(prot);
if (pmonkenum != null) Marshal.ReleaseComObject(pmonkenum);
}
return null;
}
另外,可以不引⽤COM对象,直接使⽤GetType().InvokeMember执⾏Excel操作。
⼆、DSOFramer
这种⽅法⽐较简单,感觉是对WebBrowser的封装。
/// 需要下载控件。并regsvr32注册控件。
/
// 然后添加到⼯具箱ToolBox中使⽤。
三、OWC
需要下载并安装OWC11,添加Spreadsheet到⼯具箱中即可使⽤。
OWC⽅式只能打开xml、csv、htm格式的Excel⽂件!!⽆法打开xls⽂件。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论