手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可。
实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0
另外当然还需要引用Interop.Word.Dll.
代码如下:
调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去
然后就OK了,在对标签表控制要注意列循环和换行.
实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0
另外当然还需要引用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![]()
19
Object filename = FileName;
20
Object ConfirmConversions = false;
21
Object ReadOnly = true;
22
Object AddToRecentFiles = false;
23![]()
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![]()
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![]()
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![]()
56
}
57
#endregion
///#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

19
Object filename = FileName; 20
Object ConfirmConversions = false; 21
Object ReadOnly = true; 22
Object AddToRecentFiles = false; 23

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

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

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

56
} 57
#endregion 调用上面静态方法,打开目标文件并且把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![]()
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![]()
24![]()
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![]()
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![]()
47
wApp.Selection.Move(ref unit,ref count);
48
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text);
49![]()
50
wApp.Selection.Move(ref unit,ref count);
51
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text);
52![]()
53
wApp.Selection.Move(ref unit,ref count);
54
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text);
55![]()
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
///#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

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

24

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

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

47
wApp.Selection.Move(ref unit,ref count); 48
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text); 49

50
wApp.Selection.Move(ref unit,ref count); 51
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text); 52

53
wApp.Selection.Move(ref unit,ref count); 54
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text); 55

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 然后就OK了,在对标签表控制要注意列循环和换行.



浙公网安备 33010602011771号