使用C#操作word模板

       前段时间在做项目时,系统中有一个功能模块,内容是在线填写资料并保存成word文档,当时在网上搜寻了很久,结合自己的一些实践,特定整理成一篇技术博。

首先,我们先制作完一份word模板文件。

       ①打开word2010,制作如下表格→插入书签→保存成word模板文档(Student.dot):

                                         制作表格

                                    插入书签

②打开vs2010 ,建立一个网页,命名为print,视图如下:

③添加引用 Microsoft.Office.Interop.Word:

添加完引用后,便可开始编码,代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web;
 4 using System.Web.UI;
 5 using System.Web.UI.WebControls;
 6 using System.Data.SqlClient;
 7 using System.Data;
 8 using Microsoft.Office;
 9 using Microsoft.Office.Interop.Word;
10 using Microsoft.Office.Interop;
11 using System.Text;
12 
13 public partial class print : System.Web.UI.Page 
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17 
18     }
19     protected void btprint_Click(object sender, EventArgs e)
20     {
21         Toprint();
22     }
23     public void Toprint()
24     {
25     WriteIntoWord wiw = new WriteIntoWord();
26         string FilePath = Server.MapPath("Student.dot");     //模板路径 
27       string  BookmarkName = "Name";
28         string FillName =name.Text ;
29         string BookmarkGender = "Gender";
30         string  FillGender =gender.Text;
31         string BookmarkBirthday = "Birthday";
32         string  FillBirthday =birth.Text; ;
33         string  SaveDocPath = Server.MapPath("Student.doc"); ;
34         wiw.OpenDocument(FilePath) ;
35         wiw.WriteIntoDocument(BookmarkName, FillName);
36         wiw.WriteIntoDocument(BookmarkGender, FillGender);
37         wiw.WriteIntoDocument(BookmarkBirthday, FillBirthday);
38         wiw.Save_CloseDocument(SaveDocPath) ;
39     }
40     public class  WriteIntoWord 
41 {
42     ApplicationClass app = null;   //定义应用程序对象 
43     Document doc = null;   //定义 word 文档对象
44     Object missing = System.Reflection.Missing.Value; //定义空变量
45     Object isReadOnly = false;
46    // 向 word 文档写入数据 
47     public void OpenDocument(string FilePath) 

48    {   
49         object filePath =  FilePath;//文档路径
50         app = new ApplicationClass(); //打开文档 
51         doc = app.Documents.Open(ref filePath, ref missing, ref missing, ref missing, 
ref missing, ref missing, ref missing, ref missing); 52 doc.Activate();//激活文档 53 } 54 /// <summary> 55 /// </summary> 56 ///<param name="parLableName">域标签</param> 57 /// <param name="parFillName">写入域中的内容</param> 58 /// 59 //打开word,将对应数据写入word里对应书签域 60 61 public void WriteIntoDocument(string BookmarkName, string FillName) 62 { 63 object bookmarkName = BookmarkName; 64 Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName);//返回书签 65 bm.Range.Text = FillName;//设置书签域的内容 66 } 67 /// <summary> 68 /// 保存并关闭 69 /// </summary> 70 /// <param name="parSaveDocPath">文档另存为的路径</param> 71 /// 72 public void Save_CloseDocument(string SaveDocPath) 73 { 74 object savePath = SaveDocPath; //文档另存为的路径 75 Object saveChanges = app.Options.BackgroundSave;//文档另存为 76 doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing); 77 doc.Close(ref saveChanges, ref missing, ref missing);//关闭文档 78 app.Quit(ref missing, ref missing, ref missing);//关闭应用程序 79 80 } 81 } 82 }

编辑完成测试如下:

在项目文件中打开生成的Student.doc文件;

测试完毕,成功的生成了我们要的word文档,这是简单的例子,如果需要生成大量的文档,只需简单修改一下代码就可以了。

posted @ 2013-01-26 19:17  ChenSipenG  阅读(10632)  评论(2编辑  收藏  举报