.NET 动态向Word文档添加数据

简介:     本文章主要用于在网页上填写数据动态填入Word模板中使用  首先要准备一个Word模板,然后在需要插入数据的位置插入书签,这样可以确定在网页上填入的数据可以插入到Word文档相应的位置。   在项目中要声明 using Microsoft.

    本文章主要用于在网页上填写数据动态填入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(); 
        }
    }
}

  本文用于以后操作时使用,如有不足指出望读者指出

  

 

目录
相关文章
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
|
3月前
|
网络协议 定位技术 网络安全
IPIP.NET-IP地理位置数据
IPIP.NET 是一家专注于 IP 地理位置数据的提供商,基于 BGP/ASN 数据与全球 800+ 网络监测点技术,提供高精度的 IPv4 和 IPv6 定位服务。其核心服务包括地理位置查询、详细地理信息和网络工具等,广泛应用于网络安全、广告营销、CDN 优化等领域。数据覆盖全球,支持多语言,每日更新确保实时性。IPIP.NET 提供 API 接口、离线数据库及多种语言 SDK,方便开发者集成使用。
389 0
|
6月前
|
开发框架 数据可视化 .NET
.NET 中管理 Web API 文档的两种方式
.NET 中管理 Web API 文档的两种方式
90 14
|
7月前
|
Linux C# Windows
.NET使用MiniWord高效生成Word文件
.NET使用MiniWord高效生成Word文件
173 12
|
9月前
|
SQL XML 关系型数据库
入门指南:利用NHibernate简化.NET应用程序的数据访问
【10月更文挑战第13天】NHibernate是一个面向.NET的开源对象关系映射(ORM)工具,它提供了从数据库表到应用程序中的对象之间的映射。通过使用NHibernate,开发者可以专注于业务逻辑和领域模型的设计,而无需直接编写复杂的SQL语句来处理数据持久化问题。NHibernate支持多种数据库,并且具有高度的灵活性和可扩展性。
130 2
|
8月前
|
API C#
在.NET中使用QuestPDF高效地生成PDF文档
在.NET中使用QuestPDF高效地生成PDF文档
133 0
|
11月前
|
开发框架 .NET 数据库连接
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
闲话 Asp.Net Core 数据校验(三)EF Core 集成 FluentValidation 校验数据例子
158 1
|
12月前
|
开发框架 JSON 前端开发
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
|
11月前
|
开发框架 前端开发 算法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
156 0