运用Edraw为WPF应用程序嵌入Office文档

本文介绍了如何利用Edraw Office Viewer Component在WPF应用程序中嵌入Word、Excel和PowerPoint文档。该组件提供了一种简单的方法来实现文档的展示,并提供了示例代码和步骤说明,包括添加User Control、引用组件库、编写C#代码以及设置按钮功能。通过调整解决方案平台为x86并运行,可以成功实现Office文档的嵌入。
摘要由CSDN通过智能技术生成

如何将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示例项目。

安装地址:https://www.evget.com/product/2071/download

打开Visual Studio并创建一个新的WPF应用程序。右键单击WpfApplication1解决方案。然后单击“Add”菜单并选择“User Control...”。

wpf项目中将会增加一个新的窗体。选择“User Control”项。不是“User Control(WPF)”项。双击解决方案面板中的UserControl1.CS。打开“工具箱”面板,然后单击菜单中的“ Choose Items...”。

在弹出的“Choose Toolbox Items”对话框中,选择“Edraw Office Viewer Component”,然后单击“确定”。

现在,Edraw Office Viewer Component已添加到工具箱的“常规”选项卡中。在UserControl窗体中拖动它。

通过Visual Studio将AxEDofficeLib和EDOfficeLib添加到该解决方案中。

输入打开word文档的C#代码,并保护word文档的修改如下所示:

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选项。然后建立并运行。

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");}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值