Embedding MS Word, Excel, PowerPoint into a WPF Application
How to embed MS Word, Excel, PowerPoint into a WPF application? I suppose most of you remember the good old OLE technology which allowed embedding Excel diagrams into Word documents etc. But the technology doesn't support all the Microsoft office document. It doesn't support the multiple MS Word instance in a form. Edraw office viewer component, as the alternatives on the internet, is the simplest and most reliable solution allows the developers to host MS Word documents, Excel worksheets, PowerPoint presentations into a WPF application.
Using this WPF component the developers can embed MS Word, MS Excel, MS PowerPoint into WPF application by placing an instance of Office Viewer onto the main form.
Click Here to Download Office Component - Support MS Word, Excel, PowerPoint, Visio, Project for WPF Program.
The following article will demo how to embed a MS word in a wpf application step by step.
If you haven't the officeviewer.ocx file, you need TO install the package firstly. In the component install folder, you can also find the wpf sample project.
Open the Visual Studio and create a new WPF application.
Right Click the WpfApplication1 Solution. Then click Add menu and point the User Control...
A New window form will be added in the wpf project.
Choose the "User Control" item. Not the "User Control (WPF)" item.
Double click the UserControl1.CS in the Solution panel.
Open the Toolbox panel, then click the Choose Items... in the context menu.
In the pop up Choose Toolbox Items dialog, select the Edraw Office Viewer Component then click the Ok.
Now the Edraw Office Viewer Component was added in the General tab in the Toolbox. Drag it in the UserControl form.
The AxEDofficeLib and EDOfficeLib will be added into the solution by the Visual Studio wizard.
Type the following C# codes for opening a word document and protect the word document from modification looked like this:
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();
}
}
}
At last, you need to write a host window for the UserControl. Switch to the Windows1.xaml file then add the open, protect, print and close button as the following image.
Add the following c# code to associate the 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();
}
}
}
Open the Configuration Manager. Change the Active solution platform as x86 option. Then build and run.
The office viewer component support all versions of MS Word. To embed MS Excel or PowerPoint, Visio, Project into a WPF application, you needn't change anything, only call the Open method as follow:
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");
}
Embedding MS Office in ASP.NET Program
An Easy Way to Embed Excel in a Web Page