根据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, 
52            ref missing, ref missing, ref missing, ref missing); 
53         doc.Activate();//激活文档
54     } 
55     /// <summary> 
56     /// </summary> 
57     ///<param name="parLableName">域标签</param> 
58     /// <param name="parFillName">写入域中的内容</param> 
59     /// 
60     //打开word,将对应数据写入word里对应书签域
61 
62 public void WriteIntoDocument(string BookmarkName, string FillName)
63 {
64     object bookmarkName = BookmarkName;
65     Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName);//返回书签 
66     bm.Range.Text = FillName;//设置书签域的内容
67 } 
68 /// <summary> 
69 /// 保存并关闭 
70 /// </summary> 
71 /// <param name="parSaveDocPath">文档另存为的路径</param>
72 /// 
73   public void  Save_CloseDocument(string  SaveDocPath) 
74 { 
75 object  savePath = SaveDocPath;  //文档另存为的路径 
76 Object saveChanges = app.Options.BackgroundSave;//文档另存为 
77 doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing,
78    ref missing, ref missing, ref missing);
79 doc.Close(ref saveChanges, ref missing, ref missing);//关闭文档
80 app.Quit(ref missing, ref missing, ref missing);//关闭应用程序
81       
82 } 
83 }
84 }
原文地址:https://www.cnblogs.com/itzhanghb/p/3626236.html