運用Edraw爲WPF應用程序嵌入Office文檔的方法總結

具體描述了運用Edraw Office Viewer Component爲WPF應用長鬚嵌入MS Word,Excel以及Power Point的方法。c#

打開Visual Studio,並建立一個新的WPF應用程序。工具

右鍵單擊WpfApplication1 Solution。 而後單擊Add添加菜單,並點擊User Control…spa

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030021.jpg">code

wpf的項目中將會出現一個新窗口。在Solution面板雙擊UserControl1.CS。orm

打開Toolbox面板,而後單擊Choose Items…。ci

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030022.jpg">文檔

在彈出的Choose Toolbox Items選擇工具箱項目對話框中,選擇Edraw Office Viewer Component組件而後單擊Ok。get

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030023.jpg">it

Edraw Office Viewer Component組件就已經被添加到Toolbox工具箱中添加工具箱的General選項卡中。以後將它拖放到UserControl窗口。io

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030024.jpg">

這個AxEDofficeLib和EDOfficeLib將經過Visual Studio嚮導被添加到解決方案中。

鍵入如下的c#代碼,打開一個word文檔,並保護該Word文檔不被修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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文件而後加入開放、保護、打印和關閉按鈕,以下圖所示:

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030025.jpg">

添加以下代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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();
}
}
}

打開Configuration Manager配置管理器。改變 Active解決方案平臺爲x86選項。而後構建並運行。

<IMG style="BOX-SIZING: border-box; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; VERTICAL-ALIGN: middle; BORDER-TOP: 0px; BORDER-RIGHT: 0px" alt=運用Edraw爲WPF應用程序嵌入Word/Excel/PPT的方法總結 src="http://image.evget.com/images/article/20121030026.jpg">

Office Viewer Component組件支持全部版本MS Word。嵌入MS Excel或PowerPoint,Visio、項目到一個WPF應用程序中,您沒必要改變任何東西,只能調用Open方法,以下所示:

1
2
3
4
5
6
7
8
9
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" );
}
相關文章
相關標籤/搜索