如何将Word、Excel、PowerPoint嵌入WPF应用程序?大部分人应该都记得能够将Excel图表嵌入到Word文档中的OLE技术,但该技术并不支持全部的Microsoft Office文档。它不支持表单中的多个MS Word实例。而Edraw office viewer component对于开发人员能够说是最简单的能够将Word文档、Excel工做表、PowerPoint演示文稿嵌入到WPF应用程序中的解决方案。工具
本文将演示如何逐步嵌入MS Word到wpf应用程序中。若是你没有officeviewer.ocx文件,请先安装。在组件安装文件夹中,你还能够找到wpf示例项目。spa
打开Visual Studio并建立一个新的WPF应用程序。
右键单击WpfApplication1解决方案。而后单击“Add”菜单并选择“User Control...”。code
wpf项目中将会增长一个新的窗体。
选择“User Control”项。不是“User Control(WPF)”项。
双击解决方案面板中的UserControl1.CS。
打开“工具箱”面板,而后单击菜单中的“ Choose Items...”。component
在弹出的“Choose Toolbox Items”对话框中,选择“Edraw Office Viewer Component”,而后单击“肯定”。orm
如今,Edraw Office Viewer Component已添加到工具箱的“常规”选项卡中。在UserControl窗体中拖动它。教程
经过Visual Studio将AxEDofficeLib和EDOfficeLib添加到该解决方案中。
输入打开word文档的C#代码,并保护word文档的修改以下所示:ip
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace WpfApplication1 { public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public void Open() { axEDOffice1.OpenFileDialog(); } public void Protect() { if (axEDOffice1.GetCurrentProgID() == "Word.Application") { axEDOffice1.ProtectDoc(2); } } public void Print() { axEDOffice1.PrintPreview(); } public void Close() { axEDOffice1.ExitOfficeApp(); } } }
最后,你须要为UserControl编写主机窗口。切换到Windows1.xaml文件,而后添加打开、保护、打印和关闭的按钮,如图所示。开发
添加如下c#代码来关联office component。文档
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Open_Click(object sender, RoutedEventArgs e) { _host.Open(); } private void Protect_Click(object sender, RoutedEventArgs e) { _host.Protect(); } private void Print_Click(object sender, RoutedEventArgs e) { _host.Print(); } private void Close_Click(object sender, RoutedEventArgs e) { _host.Close(); } } }
打开配置管理器。将Active Solution平台更改成x86选项。而后创建并运行。get
Office Viewer component支持全部版本的MS Word。要将MS Excel或PowerPoint Visio、Project嵌入到WPF应用程序中,你只须要调用Open方法,以下所示:
public void Open() { //axEDOffice1.OpenFileDialog(); axEDOffice1.Open(sPath, "Word.Application"); axEDOffice1.Open(sPath, "Excel.Application"); axEDOffice1.Open(sPath, "PowerPoint.Application"); axEDOffice1.Open(sPath, "Visio.Application"); axEDOffice1.Open(sPath, "MSProject.Application"); }
以上就是本次教程的所有内容,接下来会有更多相关教程,敬请关注!您也能够在评论者留下你的经验和建议。