天天看點

.NET 動态向Word文檔添加資料

    本文章主要用于在網頁上填寫資料動态填入Word模闆中使用

  首先要準備一個Word模闆,然後在需要插入資料的位置插入書簽,這樣可以确定在網頁上填入的資料可以插入到Word文檔相應的位置。

   在項目中要聲明 using Microsoft.Office.Interop.Word 類

背景代碼:

     protected void btnPrint_Click(object sender, EventArgs e)  
        {
         string path = Server.MapPath("~\\UploadFiles\\");           //解決方案下的檔案夾
            string templatePath = path + "VATInvoiceDocument.doc";      //模闆
            WordOp wop = new WordOp();                                  //執行個體化WordOp類
            wop.OpenTempelte(templatePath);
            wop.FillLable("gongsimingcheng", conType);
            wop.FillLable("huming", this.txtAccountName.Value);
            wop.FillLable("shuihao", this.txtDutyPparagraph.Value);
            wop.FillLable("kaihuhang", this.txtBankAccount.Value);
            wop.FillLable("zhanghao", this.txtAccounts.Value);
            wop.FillLable("dizhi", this.txtAddress.Value);
            wop.FillLable("dianhua", this.txtTelephone.Value);
            wop.FillLable("kaipiaodaima", this.txtBilingCode.Value); 
            wop.FillLable("shenqingrenyuan", this.txtApplicant.Value);
            wop.FillLable("lianxidianhua", this.txtContact.Value);
            wop.FillLable("nian", this.txtYearL.Value);
            wop.FillLable("yue", this.txtMonthL.Value);
            wop.FillLable("ri", this.txtDaysL.Value);
            wop.SaveAs(path + "VATInvoiceDocument1.doc", true);         //将要儲存到的Word文檔
            wop.Quit();
            Response.Redirect(@"/UploadFiles/VATInvoiceDocument1.doc"); //做個跳轉用于下載下傳. 
        }      

WordOp類的代碼實作:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace CCIR.CorpWebSite.WebPage
{
    public class WordOp
    {
        public WordOp()
        {
            // 
            // TODO: 在此處添加構造函數邏輯 
            // 
        }
        private ApplicationClass WordApp;
        private Document WordDoc;
        private static bool isOpened = false;//判斷word模版是否被占用 
        public void SaveAs(string strFileName, bool isReplace)
        {
            if (isReplace && File.Exists(strFileName))
            {
                File.Delete(strFileName);
            }
            object missing = Type.Missing;
            object fileName = strFileName;
            WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }
        //定義一個Word.Application 對象 
        public void activeWordApp()
        {
            WordApp = new ApplicationClass();
        }
        public void Quit()
        {
            object missing = System.Reflection.Missing.Value;
            WordApp.Application.Quit(ref missing, ref missing, ref missing);
            isOpened = false;
        }
        //基于模版建立Word檔案 
        public void OpenTempelte(string strTemppath)
        {
            object Missing = Type.Missing;
            //object Missing = System.Reflection.Missing.Value; 
            activeWordApp();
            WordApp.Visible = false;
            object oTemplate = (object)strTemppath;
            try
            {
                while (isOpened)
                {
                    System.Threading.Thread.Sleep(500);
                }
                WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
                isOpened = true;
                WordDoc.Activate();
            }
            catch (Exception Ex)
            {
                Quit();
                isOpened = false;
                throw new Exception(Ex.Message);
            }
        }
        public void FillLable(string LabelId, string Content)
        {
            //打開Word模版 
            // OpenTempelte(tempName); //對LabelId的标簽進行填充内容Content,即函件題目項 
            object bkmC = LabelId;
            if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
            {
                WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
            }
            WordApp.Selection.TypeText(Content);
            //SaveAs(saveAsFileName); 
            //Quit(); 
        }
    }
}      

  本文用于以後操作時使用,如有不足指出望讀者指出

  

繼續閱讀