.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(); 
        }
    }
}

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

  

 

目录
相关文章
|
2月前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
9 0
|
23天前
|
存储 测试技术 计算机视觉
高维数据惩罚回归方法:主成分回归PCR、岭回归、lasso、弹性网络elastic net分析基因数据
高维数据惩罚回归方法:主成分回归PCR、岭回归、lasso、弹性网络elastic net分析基因数据
|
2月前
|
SQL 数据库
使用ADO.NET查询和操作数据
使用ADO.NET查询和操作数据
12 0
|
3月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
39 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
3月前
|
SQL 开发框架 .NET
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
72 0
|
5月前
|
Oracle 关系型数据库 数据管理
.NET医院检验系统LIS源码,使用了oracle数据库,保证数据的隔离和安全性
LIS系统实现了实验室人力资源管理、标本管理、日常事务管理、网络管理、检验数据管理(采集、传输、处理、输出、发布)、报表管理过程的自动化,使实验室的操作人员和管理者从繁杂的手工劳作中解放出来,提高了检验人员的工作效率和效益,降低了劳动成本和差错发生率。
|
7月前
|
前端开发 JavaScript
.net core 前端传递参数有值 后端接收到的数据却是null
1、问题分析 在做接口测试时,偶然出现了前端输出有值,但是后端断点调试时却出现接收参数总是为null的情况 2、解决办法 前端打印log,看前端的每一个传值的数据类型,与后端请求参数类进行认真的一一比对 小技巧: ① 直接打印调用接口的传参值的数据类型,例如 console.log(type of this.form.name) --string console.log(type of this.form.age) --number 打印的数据类型与后端接口的参数类比对,查出不对应的类型 ② 关于非必填的值,默认传值可能出现空字符串(' ')、NaN值(Not a Number
112 0
|
8月前
|
JSON 数据格式
.NET Core - 配置绑定:使用强类型对象承载配置数据
.NET Core - 配置绑定:使用强类型对象承载配置数据
|
10月前
|
数据库 C#
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
227 0
|
12月前
|
数据采集 JavaScript 前端开发
为什么用Python爬取网页数据,在检查net work中很多和教程上不一样?
今天就来说说,我们为什么会出现这个问题,以及我们应该怎么做,才能解决这个问题?