Source: http://www.evget.com/zh-CN/Info/catalog/17967.html
The following describes how to embed MS Word, Excel, and Power Point into a WPF application using edraw office viewer component.
Open Visual Studio and create a new WPF application.
Right-click wpfapplication1 solution. Click Add to add the menu, and then click User Control...
A new window will appear in the WPF project. On the Solution panel, double-click usercontrol1.cs.
Open the Toolbox panel, and click choose items ....
In the pop-up choose toolbox items select Toolbox project dialog box, select the edraw office viewer component and click OK.
The edraw office viewer component has been added to the General tab of the Toolbox toolbox. Then drag it to the usercontrol window.
This axedofficelib and edofficelib will be added to the solution through the Visual Studio wizard.
Type the following C # code to open a Word document and prevent it from being modified:
12345678910111213141516171819202122232425262728293031323334353637 |
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();
} } } |
Finally, you need to write a host window for usercontrol. Switch to the windows1.xaml file and add the open, secure, print, and close buttons, as shown in:
Add the following code:
123456789101112131415161718192021222324252627282930313233343536373839 |
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 Configuration Manager. Change the active solution platform to the x86 option. Then build and run.
The Office viewer component supports all versions of MS Word. Embedded in MS Excel or PowerPoint, Visio, project to a WPF application, you do not have to change anything, you can only call the open method, as shown below:
123456789 |
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" );
} |