日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

『转载』在vs2008(2005)winform中,打开office文档

發(fā)布時(shí)間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 『转载』在vs2008(2005)winform中,打开office文档 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近在準(zhǔn)備畢業(yè)設(shè)計(jì),這個(gè)階段應(yīng)該是可行性分析階段吧,在查閱相關(guān)的技術(shù)問(wèn)題,由于涉及office,所以今天寫下這篇文章,以備日后查閱。這篇文章也是參閱msdn而來(lái)的,我在這里提供了實(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 或 Visual C# .NET 中新建一個(gè) Windows 應(yīng)用程序項(xiàng)目。默認(rèn)情況下創(chuàng)建 Form1。

    注意:在 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ǔ)言運(yùn)行庫(kù)代理 (SHDocVw.dll) 和 Windows 窗體代理 (AxSHDocVw.dll)。若要在 Visual C# 2005 中添加 DLL 文件,請(qǐng)按下列步驟操作:
  • 在“項(xiàng)目”菜單上,單擊“添加引用”。
  • 在“添加引用”對(duì)話框中,單擊“瀏覽”。
  • 找到并選擇 AxSHDocVw.dll 和 SHDocVw.dll 文件。
  • 若要為這兩個(gè)文件添加項(xiàng)目引用,請(qǐng)單擊“確定”。
  • 在“工具”菜單上,單擊“自定義工具箱”以打開“自定義工具箱”對(duì)話框。在“COM 組件”選項(xiàng)卡上,添加一個(gè)對(duì)“Microsoft 瀏覽器”的引用。單擊“確定”,將 WebBrowser 控件添加到 Windows 窗體工具箱。WebBrowser 控件會(huì)顯示出來(lái),并且在工具箱中帶有“Explorer”(資源管理器)字樣。

    注意:在 Visual Studio 2005 中,不必執(zhí)行步驟 2。編者按:我采用2008時(shí),好幾次沒(méi)有成功的添加這個(gè)控件,建議先刪除工具欄中原來(lái)的WebBrower控件。
  • 使用該工具箱向 Form1 添加一個(gè) WebBrowser 控件、一個(gè) OpenFileDialog 控件和一個(gè) CommandButton 控件。這就會(huì)向 Form1 類添加“AxWebBrowser1”、“OpenFileDialog1”和“Button1”成員變量。在 Visual C# 2005 中,會(huì)添加“webBrowser1”、“openFileDialog1”和“button1”成員變量。
  • 在 Form1 上,雙擊“Button1”。這就會(huì)向 Form1 添加”Button1_Click”事件。
  • 在 Form1 的代碼窗口中,向列表添加以下命名空間: using System.Reflection;
  • 如下所示在 Form1 類中定義一個(gè)私有成員: private Object oDocument;
  • 在 Form1 類的“InitializeComponent”方法的末尾,添加以下代碼以處理“Form1_Load”、“Form1_Closed”和“axWebBrowser1_NavigateComplete2”事件: 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, System.EventArgs e) { } 替換為: private void button1_Click(object sender, System.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);} }public void Form1_Load(object sender, System.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() ); }
  • 完整的代碼如下: Code
    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)通過(guò)從工具箱拖放控件所執(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ù)器名稱的消息框。
  • 下載該項(xiàng)目文件以及前文提到的兩個(gè)文件
  • 編者按:你的電腦必須安裝了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)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。