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

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

  

.NET 动态向Word文档添加数据相关推荐

  1. 整理关于Java进行word文档的数据动态数据填充

    首先我们看下,别人整理的关于Java生成doc 的 资料. java生成word的几种方案 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用 ...

  2. Freemarker - 根据模板动态生成word文档

    文章目录 Freemarker 根据模板动态生成word文档 Freemarker 介绍: Freemarker 使用: freemarker加载模板目录的方法 参考资料 Freemarker 根据模 ...

  3. Springboot中使用freemarker动态生成word文档

    文章目录 freemarker模板动态生成word文档 前言 准备 简单模板准备 <一> `word 2003` 新建`.doc` 模板 <二> 另存为`.xml` 文件,格式 ...

  4. java 操作word中表格_Java 使用Spire.Cloud.Word给Word文档添加表格

    在编辑Word文档时,很多时候需要用到表格,以便能够清晰整洁地表达和归类数据.本文就将介绍如何使用Spire.Cloud.Word给Word文档添加表格.Spire.Cloud.Word提供了Tabl ...

  5. word文档添加目录

    当需要给word文档添加目录时,当需要查看word文档结构图(即左边显示目录--点击目录,右边自动跳转至目录所在页)时, 第一步,你需要先把希望显示的文字,设置其样式为"标题",才 ...

  6. word文档添加多级目录,自动生成目录

    word文档添加多级目录 转载:http://jingyan.baidu.com/article/8ebacdf0c32c5f49f65cd53f.html Word如何添加一级二级标题等多级标题 有 ...

  7. Java实现在线word文档添加铺满效果文字水印,文字可换行

    Java实现在线word文档添加铺满效果文字水印,文字可换行 使用插件Aspose.words for java 生成水印的工具类 调用生成水印方法 需求:为在线预览的word文档添加自定义水印,还要 ...

  8. Python动态修改Word文档内容,保留格式样式,并批量生成PDF

    Python动态修改Word文档内容,保留格式样式,并批量生成PDF 前言 一.需要安装的库 二.核心逻辑-替换 前言 假如你有一个Word模版文档,要在里面填写人员信息,但人员有成百上千个,手动填起 ...

  9. word 技巧之一:给word文档添加目录、查看word文档结构图

    当需要给word文档添加目录时,当需要查看word文档结构图(即左边显示目录--点击目录,右边自动跳转至目录所在页)时, 第一步,你需要先把希望显示的文字,设置其样式为"标题",才 ...

最新文章

  1. MindSpore API编程概述
  2. python函数都有啥_什么是python函数
  3. 腾讯AR开放平台今日正式开放,提供识别、追踪、展现等多项能力
  4. Linux中增加软路由的两种方法,Linux中增加软路由的三种方法
  5. 在JAX-RS中处理异步请求中的超时
  6. Nacos服务发现实践
  7. SpringCloud 服务网关 Zuul 自定义路由和排除路由配置
  8. 【踩坑】activiti工作流的svg-xml解析报错
  9. html中不支持什么元素,html5新元素和html不支持的元素
  10. kindlefire刷安卓系统_Kindle Fire平板刷入原生Android 4.0系统教程
  11. 浙江大学求是科学班计算机,浙江大学竺可桢学院求是科学班、智慧能源班、公共管理英才班2018年招生简章...
  12. 解决IntelliJ IDEA中打开JSP文件(使用快捷键Alt+F2)时,弹出的浏览器网页只显示JSP源码
  13. 计算机输入法不能切换用户登录,输入法不能正常切换,输入法切换不出来 - 输入法切换不了,输入法不能切换怎么办? - 安全专题...
  14. LTE学习笔记四:OFDM
  15. 解决win10升级补丁后共享打印机0x0000011b报错问题最简单解决方法,不需要卸载补丁
  16. 网站中的js代码欢迎语,早上好,下午好……
  17. d3dcompiler_47.dll缺失怎么修复?D3DCompiler_47.dll修复工具下载
  18. 中国石油大学北京计算机专业保研,2020年中国石油大学(北京)保研率是多少...
  19. JavaScript循环语句的性能问题
  20. 计算机学科专业基础综合简称,2010年考研北京大学计算机学科专业基础综合(408)重难点解析...

热门文章

  1. 人类应鼎力进行探索的35种颠覆性技术
  2. 地理信息技术加持 用数字孪生让城市更“聪明”
  3. Science公布:全世界最前沿的125个科学问题
  4. 潘建伟团队首次实现18个光量子比特纠缠,刷新世界记录
  5. 漫画:产品经理的这些“谎言”,你一定中招过!!!
  6. 如何惹毛一名开发者?
  7. 可视化经验分享 | 赠书
  8. 揭秘鸿蒙系统中的 JS 开发框架
  9. UOJ#179. 线性规划(线性规划)
  10. Redis架构第二天:CenterOS集群、RDB和AOF、主从复制架构实践