最近在准备毕业设计,这个阶段应该是可行性分析阶段吧,在查阅相关的技术问题,由于涉及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 的代码窗口中,向列表添加以下命名空间:
    using System.Reflection;

  6. 如下所示在 Form1 类中定义一个私有成员:
    private Object oDocument; 

  7. 在 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);

  8. 将下面的代码
    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() );
    }

  9. 完整的代码如下:
    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 文档服务器名称的消息框。

  10. 下载该项目文件以及前文提到的两个文件

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

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

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

  1. PageOffice如何控制在系统中打开Word文档只读

    PageOffice如何控制在系统中打开Word文档只读 在文档系统有些环节需要限制用户编辑word文件,只许查看,那么如何实现用程序控制文件打开的时候,用只读模式打开呢?若通过PageOffice开 ...

  2. 文档预览 OfficeWebViewer:在浏览器中查看Office文档

    Office Web Viewer:在浏览器中查看Office文档 由办公室团队 即使您的读者没有安装Office,您的网站或博客上是否也有要阅读的Office文档?您宁愿先查看文档再下载吗?为了给您 ...

  3. 调试经验——使用VBA在Excel中打开Word文档(Open Word file in Excel with VBA)

    VBA的一个强项是可以跨应用操作,特别是在office各程序内部.以下代码实现了在Excel中打开Word文档. 代码: Sub openWord() '在Excel中打开Word文档 ' 'Dim ...

  4. 服务器word文件病毒,极具欺骗性的勒索病毒出现!打开Office文档立刻中招

    WannaCry.Petya.Cerber.Locky.Spora--勒索病毒如今已经泛滥成灾,下半年几乎每天都有新的变种出现,攻击手法也无所不用其极.中了这种病毒后,用户的电脑文件就会被高强度加密, ...

  5. 在Word2003中打开Word2007文档的方法及教程下载

    1.到微软网站下载以下工具: http://download.microsoft.com/download/6/9/E/69EA942D-4636-4350-A526-0BFD9771A12A/O20 ...

  6. 在web网页中打开word文档

    1:首先添加下引用,右击网站→添加引用→弹出的框COM下添加 using Microsoft.Office.Interop.Word; using System; using System.Colle ...

  7. 使用windows activeX 在Webclient UI 中打开word文档

    Created by Jerry Wang, last modified on May 23, 2014 在view page是使用如下source code: 在UI component workb ...

  8. 网页中打开word文档

    前台代码打开: <html> <head> <title>my youth</title> <meta http-equir="cont ...

  9. java在浏览器打开word,如何直接在浏览器中打开word文档

    acid when i run this the word document is not opening in a browser 解决方案 By default browser does not ...

最新文章

  1. js 定时任务,定时器
  2. 线下生意再次“受宠”:大数据给你添点料
  3. EventBus VS Spring Event
  4. c语言课程设计怎么做,C语言课程设计————写下流程图! 谢谢
  5. view类不响应自定义消息_安卓平台如何给控件添加自定义操作?
  6. 学习vi和vim编辑器(1):vi文本编辑器
  7. 用java读取word2003和word2007的内容
  8. 09年全年的case处理总量
  9. Mac在github中管理自己的代码(入门篇)
  10. Spring中三种编程式事务的使用
  11. springboot+vue整合百度的Ueditor(保姆级教程)
  12. html 播放amr ios,关于iOS设备播放amr格式的音频文件
  13. 多个mysql共存_双mysql共存(MySQL8.0与MySQL5.7)
  14. DEFCON 23即将展开物联网“大屠杀”
  15. Scala学习视频心得(一)语言特点、伴生对象
  16. 出现“你的Windows许可证即将过期”提示
  17. iPhone Plus手机的分辨率到底是多少,是1080×1920还是1242×2208?
  18. Filament 渲染引擎剖析 之 创建渲染对象 1
  19. MySQL经典面试题--SQL语句
  20. [SCOI2009]粉刷匠 dp

热门文章

  1. input添加disabled属性出现的移动端兼容问题
  2. Linux运行Java出现“Exception in thread main java.lang.OutOfMemoryError: Java heap space”报错...
  3. ASP实现记住密码的功能
  4. 用Python写的一个monkeyrunner小工具(支持手机截图与定时截图,手机屏幕的显示)
  5. 知乎高赞:985计算机视觉毕业后找不到工作怎么办?怒刷leetcode,还是另寻他路?
  6. Kafka参数详解及调优--生产者
  7. 并发的核心:CAS 是什么?Java8是如何优化 CAS 的?
  8. 中国SaaS死或生之一:“网红”CRM的大起大落
  9. Spring Cloud Gateway(限流)
  10. spring boot实战(第四篇)分散配置