如何將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"); }
以上就是本次教程的所有內容,接下來會有更多相關教程,敬請關注!您也能夠在評論者留下你的經驗和建議。