在vs2008(2005)winform中,打开 office文档

来自:博客园 

最近在准备毕业设计,这个阶段应该是可行性分析阶段吧,在查阅相关的技术问题,由于涉及office,所以今天写下这篇文章,以备日后查阅。这篇文 章也是参阅msdn而来的,我在这里提供了实例和下载,方便大家调试。

 

您可能希望直接在 Microsoft Visual C# 窗体中显示或嵌入 Microsoft Office 文档。Microsoft Visual C# 2005 和 Microsoft Visual C# .NET 不提供用于在窗体中嵌入 Office 文档的 OLE 控件。如果希望嵌入现有文档并将其作为 Visual C# 窗体内的就地 ActiveX 文档对象打开,一个可能的解决方案是使用 Microsoft WebBrowser 控件。

本文阐述如何使用 WebBrowser 控件在 Visual C# 窗体内浏览到现有 Office 文档并显示它。

要创建可打开 Office 文档的 Visual C# 应用程序,请按照下列步骤操作:

  1. 在 Visual C# 2005 或 Visual C# .NET 中新建一个 Windows 应用程序项目。默认情况下创建 Form1。

    注意 :在 Visual C# 2005 中,如果您找不到 SHDocVw.dll 文件或 AxSHDocVw.dll 文件,请在 Visual Studio 命令提示符下运行下面的命令:
    aximp %WINDIR%/system32/shdocvw.dll  
    







    然 后,为 Microsoft WebBrowser 控件创建公共语言运行库代理 (SHDocVw.dll) 和 Windows 窗体代理 (AxSHDocVw.dll)。若要在 Visual C# 2005 中添加 DLL 文件,请按下列步骤操作:
    1. 在“项目”菜单上,单击“添加引用”。
    2. 在“添加引用”对话框中,单击“浏览”。
    3. 找到并选择 AxSHDocVw.dll 和 SHDocVw.dll 文件。
    4. 若要为这两个文件添加项目引用,请单击“确定”。
  2. 在“工具”菜单上,单击“自定义工具箱”以打开“自定义工具箱”对话框。在“COM 组件”选项卡上,添加一个对“Microsoft 浏览器”的引用。单击“确定”,将 WebBrowser 控件添加到 Windows 窗体工具箱。WebBrowser 控件会显示出来,并且在工具箱中带有“Explorer”(资源管理器)字样。

    注意 :在 Visual Studio 2005 中,不必执行步骤 2。(编者按:我采用2008时,好几次没有 成功的添加这个控件,建议先删除工具栏中原来的WebBrower控件。 )
  3. 使用该工具箱向 Form1 添加一个 WebBrowser 控件、一个 OpenFileDialog 控件和一个 CommandButton 控件。这就会向 Form1 类添加“AxWebBrowser1”、“OpenFileDialog1”和“Button1”成员变量。在 Visual C# 2005 中,会添加“webBrowser1”、“openFileDialog1”和“button1”成员变量。
  4. 在 Form1 上,双击“Button1”。这就会向 Form1 添加”Button1_Click”事件。
  5. 在 Form1 的代码窗口中,向列表添加以下命名空间:
    DE>
    using System.Reflection;


    DE>
  6. 如下所示在 Form1 类中定义一个私有成员:
    DE>
    private Object oDocument; 
    DE>
  7. 在 Form1 类的“InitializeComponent”方法的末尾,添加以下代码以处理“Form1_Load”、“Form1_Closed”和 “axWebBrowser1_NavigateComplete2”事件:
    DE>
    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);


    DE>
  8. 将下面的代码
    DE>
    private void button1_Click(object sender, System.EventArgs e)


    {


    }


    DE>
    替换为:
    DE>
    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() );


    }
    DE>
  9. DE>
    完整的代码如下:
    DE>DE>
    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());
            }


        }
    }
    DE> 注意 :您必须在 Visual Studio 2005 中更改此代码。默认情况下,当您创建 Windows 窗体项目时,Visual C# 向该项目添加一个窗体。该窗体被命名为 Form1。表示该窗体的两个文件被命名为 Form1.cs 和 Form1.designer.cs。您在 Form1.cs 中编写代码。Windows 窗体设计器在 Form1.designer.cs 文件中编写代码,这些代码实现通过从工具箱拖放控件所执行的所有操作。

    按 F5 运行该项目。单击“浏览”后,会出现“打开”对话框,您可以使用该对话框浏览到 Word 文档、Excel 工作表或 PowerPoint 演示文稿。选择任一文件,然后单击“打开”。文档在 WebBrowser 控件内打开,并出现一个显示 Office 文档服务器名称的消息框。
  10. 下载该项目文件以及前文提到的两个文件

编者按:你的电脑必须安装了office的Excel、word、PowerPoint这三个软件,才能用这个程序打开相应的文档!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值