. NET 技术讨论

学于明志,交流增加见识,讨论改变思维
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#实现对Word文件读写

Posted on 2006-03-10 12:45    阅读(481)  评论(0编辑  收藏  举报
手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制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对象 

 8public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref Word.Application WApp) 
 9
10if(FileName == ""return
11Word.Document thisDocument = null
12Word.FormFields formFields = null
13Word.Application thisApplication = new Word.ApplicationClass(); 
14thisApplication.Visible = true
15thisApplication.Caption = ""
16thisApplication.Options.CheckSpellingAsYouType = false
17thisApplication.Options.CheckGrammarAsYouType = false
18
19Object filename = FileName; 
20Object ConfirmConversions = false
21Object ReadOnly = true
22Object AddToRecentFiles = false
23
24Object PasswordDocument = System.Type.Missing; 
25Object PasswordTemplate = System.Type.Missing; 
26Object Revert = System.Type.Missing; 
27Object WritePasswordDocument = System.Type.Missing; 
28Object WritePasswordTemplate = System.Type.Missing; 
29Object Format = System.Type.Missing; 
30Object Encoding = System.Type.Missing; 
31Object Visible = System.Type.Missing; 
32Object OpenAndRepair = System.Type.Missing; 
33Object DocumentDirection = System.Type.Missing; 
34Object NoEncodingDialog = System.Type.Missing; 
35Object XMLTransform = System.Type.Missing; 
36
37try 
38
39Word.Document wordDoc = 
40thisApplication.Documents.Open(ref filename, ref ConfirmConversions, 
41ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, 
42ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, 
43ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, 
44ref NoEncodingDialog, ref XMLTransform ); 
45
46thisDocument = wordDoc; 
47wDoc = wordDoc; 
48WApp = thisApplication; 
49formFields = wordDoc.FormFields; 
50}
 
51catch(Exception ex) 
52
53MessageBox.Show(ex.Message); 
54}
 
55
56}
 
57#endregion 

调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去
 1///#region Word填充数据(For Example) 
 2///  
 3/// Word填充数据 
 4///  

 5private void WordLoadData() 
 6
 7Word.Document wDoc=null
 8Word.Application wApp=null
 9sysFun.CreateWordDocument("E:\\监测报告(new).dot",ref wDoc,ref wApp); 
10
11//对标签"C"进行填充 
12object bkmC="C"
13if(wApp.ActiveDocument.Bookmarks.Exists("C"== true
14
15wApp.ActiveDocument.Bookmarks.get_Item 
16(ref bkmC).Select(); 
17}
 
18wApp.Selection.TypeText(this.txt1.Text); 
19object bkmG = "TWaterTable3"
20object unit;  
21object count; //移动数 
22object extend;  
23
24
25extend = Word.WdMovementType.wdExtend; 
26unit = Word.WdUnits.wdCell; 
27//把DataGrid中数据填充到标签TWaterTable3上 
28if(wApp.ActiveDocument.Bookmarks.Exists("TWaterTable3"== true
29
30wApp.ActiveDocument.Bookmarks.get_Item 
31(ref bkmG).Select(); 
32
33for(int i=0;i 
34if(i==0
35
36count=1
37}
 
38else 
39
40count=0
41}
 
42//需填充5列数据 
43wApp.Selection.Move(ref unit,ref count); 
44wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[0].Text); 
45count=1
46
47wApp.Selection.Move(ref unit,ref count); 
48wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text); 
49
50wApp.Selection.Move(ref unit,ref count); 
51wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text); 
52
53wApp.Selection.Move(ref unit,ref count); 
54wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text); 
55
56wApp.Selection.Move(ref unit,ref count); 
57wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[4].Text); 
58//换行 
59wApp.Selection.MoveRight(ref unit,ref count,ref extend); 
60}
 
61}
 
62}
 
63#endregion 

然后就OK了,在对标签表控制要注意列循环和换行.