欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/3/21 编程问答 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 『转载』在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# 应用程序,请按照下列步骤操作:

  • 在 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 文件,请按下列步骤操作:
  • 在“项目”菜单上,单击“添加引用”。
  • 在“添加引用”对话框中,单击“浏览”。
  • 找到并选择 AxSHDocVw.dll 和 SHDocVw.dll 文件。
  • 若要为这两个文件添加项目引用,请单击“确定”。
  • 在“工具”菜单上,单击“自定义工具箱”以打开“自定义工具箱”对话框。在“COM 组件”选项卡上,添加一个对“Microsoft 浏览器”的引用。单击“确定”,将 WebBrowser 控件添加到 Windows 窗体工具箱。WebBrowser 控件会显示出来,并且在工具箱中带有“Explorer”(资源管理器)字样。

    注意:在 Visual Studio 2005 中,不必执行步骤 2。编者按:我采用2008时,好几次没有成功的添加这个控件,建议先删除工具栏中原来的WebBrower控件。
  • 使用该工具箱向 Form1 添加一个 WebBrowser 控件、一个 OpenFileDialog 控件和一个 CommandButton 控件。这就会向 Form1 类添加“AxWebBrowser1”、“OpenFileDialog1”和“Button1”成员变量。在 Visual C# 2005 中,会添加“webBrowser1”、“openFileDialog1”和“button1”成员变量。
  • 在 Form1 上,双击“Button1”。这就会向 Form1 添加”Button1_Click”事件。
  • 在 Form1 的代码窗口中,向列表添加以下命名空间: using System.Reflection;
  • 如下所示在 Form1 类中定义一个私有成员: 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 中更改此代码。默认情况下,当您创建 Windows 窗体项目时,Visual C# 向该项目添加一个窗体。该窗体被命名为 Form1。表示该窗体的两个文件被命名为 Form1.cs 和 Form1.designer.cs。您在 Form1.cs 中编写代码。Windows 窗体设计器在 Form1.designer.cs 文件中编写代码,这些代码实现通过从工具箱拖放控件所执行的所有操作。

    按 F5 运行该项目。单击“浏览”后,会出现“打开”对话框,您可以使用该对话框浏览到 Word 文档、Excel 工作表或 PowerPoint 演示文稿。选择任一文件,然后单击“打开”。文档在 WebBrowser 控件内打开,并出现一个显示 Office 文档服务器名称的消息框。
  • 下载该项目文件以及前文提到的两个文件
  • 编者按:你的电脑必须安装了office的Excel、word、PowerPoint这三个软件,才能用这个程序打开相应的文档!

    转载于:https://www.cnblogs.com/longqi293/archive/2008/12/29/1364331.html

    《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

    总结

    以上是生活随笔为你收集整理的『转载』在vs2008(2005)winform中,打开office文档的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。