使用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文檔,這是簡單的例子,如果需要生成大量的文檔,只需簡單修改一下代碼就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM