手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可。
实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0
另外当然还需要引用Interop.Word.Dll.
代码如下:
调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去
然后就OK了,在对标签表控制要注意列循环和换行.
实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0
![](/images/cnblogs_com/xhp5678/05-07-26-office-1.jpg)
另外当然还需要引用Interop.Word.Dll.
代码如下:
1
///#region 打开Word文档,并且返回对象wDoc,wDoc
2
///
3
/// 打开Word文档,并且返回对象wDoc,wDoc
4
///
5
/// 完整Word文件路径+名称
6
/// 返回的Word.Document wDoc对象
7
/// 返回的Word.Application对象
8
public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref Word.Application WApp)
9
{
10
if(FileName == "") return;
11
Word.Document thisDocument = null;
12
Word.FormFields formFields = null;
13
Word.Application thisApplication = new Word.ApplicationClass();
14
thisApplication.Visible = true;
15
thisApplication.Caption = "";
16
thisApplication.Options.CheckSpellingAsYouType = false;
17
thisApplication.Options.CheckGrammarAsYouType = false;
18![](/Images/OutliningIndicators/InBlock.gif)
19
Object filename = FileName;
20
Object ConfirmConversions = false;
21
Object ReadOnly = true;
22
Object AddToRecentFiles = false;
23![](/Images/OutliningIndicators/InBlock.gif)
24
Object PasswordDocument = System.Type.Missing;
25
Object PasswordTemplate = System.Type.Missing;
26
Object Revert = System.Type.Missing;
27
Object WritePasswordDocument = System.Type.Missing;
28
Object WritePasswordTemplate = System.Type.Missing;
29
Object Format = System.Type.Missing;
30
Object Encoding = System.Type.Missing;
31
Object Visible = System.Type.Missing;
32
Object OpenAndRepair = System.Type.Missing;
33
Object DocumentDirection = System.Type.Missing;
34
Object NoEncodingDialog = System.Type.Missing;
35
Object XMLTransform = System.Type.Missing;
36![](/Images/OutliningIndicators/InBlock.gif)
37
try
38
{
39
Word.Document wordDoc =
40
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
41
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
42
ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
43
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
44
ref NoEncodingDialog, ref XMLTransform );
45![](/Images/OutliningIndicators/InBlock.gif)
46
thisDocument = wordDoc;
47
wDoc = wordDoc;
48
WApp = thisApplication;
49
formFields = wordDoc.FormFields;
50
}
51
catch(Exception ex)
52
{
53
MessageBox.Show(ex.Message);
54
}
55![](/Images/OutliningIndicators/InBlock.gif)
56
}
57
#endregion
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
57
![](/Images/OutliningIndicators/None.gif)
调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去
1
///#region Word填充数据(For Example)
2
///
3
/// Word填充数据
4
///
5
private void WordLoadData()
6
{
7
Word.Document wDoc=null;
8
Word.Application wApp=null;
9
sysFun.CreateWordDocument("E:\\监测报告(new).dot",ref wDoc,ref wApp);
10![](/Images/OutliningIndicators/InBlock.gif)
11
//对标签"C"进行填充
12
object bkmC="C";
13
if(wApp.ActiveDocument.Bookmarks.Exists("C") == true)
14
{
15
wApp.ActiveDocument.Bookmarks.get_Item
16
(ref bkmC).Select();
17
}
18
wApp.Selection.TypeText(this.txt1.Text);
19
object bkmG = "TWaterTable3";
20
object unit;
21
object count; //移动数
22
object extend;
23![](/Images/OutliningIndicators/InBlock.gif)
24![](/Images/OutliningIndicators/InBlock.gif)
25
extend = Word.WdMovementType.wdExtend;
26
unit = Word.WdUnits.wdCell;
27
//把DataGrid中数据填充到标签TWaterTable3上
28
if(wApp.ActiveDocument.Bookmarks.Exists("TWaterTable3") == true)
29
{
30
wApp.ActiveDocument.Bookmarks.get_Item
31
(ref bkmG).Select();
32![](/Images/OutliningIndicators/InBlock.gif)
33
for(int i=0;i {
34
if(i==0)
35
{
36
count=1;
37
}
38
else
39
{
40
count=0;
41
}
42
//需填充5列数据
43
wApp.Selection.Move(ref unit,ref count);
44
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[0].Text);
45
count=1;
46![](/Images/OutliningIndicators/InBlock.gif)
47
wApp.Selection.Move(ref unit,ref count);
48
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text);
49![](/Images/OutliningIndicators/InBlock.gif)
50
wApp.Selection.Move(ref unit,ref count);
51
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text);
52![](/Images/OutliningIndicators/InBlock.gif)
53
wApp.Selection.Move(ref unit,ref count);
54
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text);
55![](/Images/OutliningIndicators/InBlock.gif)
56
wApp.Selection.Move(ref unit,ref count);
57
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[4].Text);
58
//换行
59
wApp.Selection.MoveRight(ref unit,ref count,ref extend);
60
}
61
}
62
}
63
#endregion
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
62
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
63
![](/Images/OutliningIndicators/None.gif)
然后就OK了,在对标签表控制要注意列循环和换行.