『转载』在vs2008(2005)winform中,打开office文档
最近在準(zhǔn)備畢業(yè)設(shè)計(jì),這個(gè)階段應(yīng)該是可行性分析階段吧,在查閱相關(guān)的技術(shù)問題,由于涉及office,所以今天寫下這篇文章,以備日后查閱。這篇文章也是參閱msdn而來的,我在這里提供了實(shí)例和下載,方便大家調(diào)試。
?
您可能希望直接在 Microsoft Visual C# 窗體中顯示或嵌入 Microsoft Office 文檔。Microsoft Visual C# 2005 和 Microsoft Visual C# .NET 不提供用于在窗體中嵌入 Office 文檔的 OLE 控件。如果希望嵌入現(xiàn)有文檔并將其作為 Visual C# 窗體內(nèi)的就地 ActiveX 文檔對(duì)象打開,一個(gè)可能的解決方案是使用 Microsoft WebBrowser 控件。
本文闡述如何使用 WebBrowser 控件在 Visual C# 窗體內(nèi)瀏覽到現(xiàn)有 Office 文檔并顯示它。
要?jiǎng)?chuàng)建可打開 Office 文檔的 Visual C# 應(yīng)用程序,請(qǐng)按照下列步驟操作:
注意:在 Visual C# 2005 中,如果您找不到 SHDocVw.dll 文件或 AxSHDocVw.dll 文件,請(qǐng)?jiān)?Visual Studio 命令提示符下運(yùn)行下面的命令: aximp %WINDIR%\system32\shdocvw.dll 編者按:你可以在文章最后下載到這兩個(gè)文件 然后,為 Microsoft WebBrowser 控件創(chuàng)建公共語言運(yùn)行庫(kù)代理 (SHDocVw.dll) 和 Windows 窗體代理 (AxSHDocVw.dll)。若要在 Visual C# 2005 中添加 DLL 文件,請(qǐng)按下列步驟操作:
注意:在 Visual Studio 2005 中,不必執(zhí)行步驟 2。編者按:我采用2008時(shí),好幾次沒有成功的添加這個(gè)控件,建議先刪除工具欄中原來的WebBrower控件。
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?System.Reflection;
namespace?OnPPT
{
????public?partial?class?Form1?:?Form
????{
????????private?Object?oDocument;
????
????????public?Form1()
????????{
????????????InitializeComponent();
????????????this.axWebBrowser1.NavigateComplete2?+=?new?AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
????????????this.Load?+=?new?System.EventHandler(this.Form1_Load);
????????????this.Closed?+=?new?System.EventHandler(this.Form1_Closed);
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????String?strFileName;
????????????//Find?the?Office?document.
????????????openFileDialog1.FileName?=?"";
????????????openFileDialog1.ShowDialog();
????????????strFileName?=?openFileDialog1.FileName;
????????????//If?the?user?does?not?cancel,?open?the?document.
????????????if?(strFileName.Length?!=?0)
????????????{
????????????????Object?refmissing?=?System.Reflection.Missing.Value;
????????????????oDocument?=?null;
????????????????axWebBrowser1.Navigate(strFileName,?ref?refmissing,?ref?refmissing,?ref?refmissing,?ref?refmissing);
????????????}
????????}
????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????button1.Text?=?"Browse";
????????????openFileDialog1.Filter?=?"Office?Documents(*.doc,?*.xls,?*.ppt)|*.doc;*.xls;*.ppt";
????????????openFileDialog1.FilterIndex?=?1;
????????}
????????public?void?Form1_Closed(object?sender,?System.EventArgs?e)
????????{
????????????oDocument?=?null;
????????}
????????public?void?axWebBrowser1_NavigateComplete2(object?sender,?AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event?e)
????????{
????????????//Note:?You?can?use?the?reference?to?the?document?object?to?
????????????//??????automate?the?document?server.
????????????Object?o?=?e.pDisp;
????????????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);
????????????//MessageBox.Show("File?opened?by:?"?+?oName.ToString());
????????}
????}
} 注意:您必須在 Visual Studio 2005 中更改此代碼。默認(rèn)情況下,當(dāng)您創(chuàng)建 Windows 窗體項(xiàng)目時(shí),Visual C# 向該項(xiàng)目添加一個(gè)窗體。該窗體被命名為 Form1。表示該窗體的兩個(gè)文件被命名為 Form1.cs 和 Form1.designer.cs。您在 Form1.cs 中編寫代碼。Windows 窗體設(shè)計(jì)器在 Form1.designer.cs 文件中編寫代碼,這些代碼實(shí)現(xiàn)通過從工具箱拖放控件所執(zhí)行的所有操作。
按 F5 運(yùn)行該項(xiàng)目。單擊“瀏覽”后,會(huì)出現(xiàn)“打開”對(duì)話框,您可以使用該對(duì)話框?yàn)g覽到 Word 文檔、Excel 工作表或 PowerPoint 演示文稿。選擇任一文件,然后單擊“打開”。文檔在 WebBrowser 控件內(nèi)打開,并出現(xiàn)一個(gè)顯示 Office 文檔服務(wù)器名稱的消息框。
編者按:你的電腦必須安裝了office的Excel、word、PowerPoint這三個(gè)軟件,才能用這個(gè)程序打開相應(yīng)的文檔!
轉(zhuǎn)載于:https://www.cnblogs.com/longqi293/archive/2008/12/29/1364331.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的『转载』在vs2008(2005)winform中,打开office文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第8.23节 Python中使用sort
- 下一篇: UIBezierPath和CAShape