『转载』在vs2008(2005)winform中,打开office文档
生活随笔
收集整理的这篇文章主要介绍了
『转载』在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 中,如果您找不到 SHDocVw.dll 文件或 AxSHDocVw.dll 文件,请在 Visual Studio 命令提示符下运行下面的命令: aximp %WINDIR%\system32\shdocvw.dll 编者按:你可以在文章最后下载到这两个文件 然后,为 Microsoft WebBrowser 控件创建公共语言运行库代理 (SHDocVw.dll) 和 Windows 窗体代理 (AxSHDocVw.dll)。若要在 Visual C# 2005 中添加 DLL 文件,请按下列步骤操作:
注意:在 Visual Studio 2005 中,不必执行步骤 2。编者按:我采用2008时,好几次没有成功的添加这个控件,建议先删除工具栏中原来的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 中更改此代码。默认情况下,当您创建 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文档的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 第8.23节 Python中使用sort
- 下一篇: UIBezierPath和CAShape