本文章主要用于在网页上填写数据动态填入Word模板中使用

  首先要准备一个Word模板,然后在需要插入数据的位置插入书签,这样可以确定在网页上填入的数据可以插入到Word文档相应的位置。

   在项目中要声明 using Microsoft.Office.Interop.Word 类

后台代码:

  1.      protected void btnPrint_Click(object sender, EventArgs e)
  2. {
  3.      string path = Server.MapPath("~\\UploadFiles\\"); //解决方案下的文件夹
  4. string templatePath = path + "VATInvoiceDocument.doc"; //模板
  5. WordOp wop = new WordOp(); //实例化WordOp类
  6. wop.OpenTempelte(templatePath);
  7. wop.FillLable("gongsimingcheng", conType);
  8. wop.FillLable("huming", this.txtAccountName.Value);
  9. wop.FillLable("shuihao", this.txtDutyPparagraph.Value);
  10. wop.FillLable("kaihuhang", this.txtBankAccount.Value);
  11. wop.FillLable("zhanghao", this.txtAccounts.Value);
  12. wop.FillLable("dizhi", this.txtAddress.Value);
  13. wop.FillLable("dianhua", this.txtTelephone.Value);
  14. wop.FillLable("kaipiaodaima", this.txtBilingCode.Value);
  15. wop.FillLable("shenqingrenyuan", this.txtApplicant.Value);
  16. wop.FillLable("lianxidianhua", this.txtContact.Value);
  17. wop.FillLable("nian", this.txtYearL.Value);
  18. wop.FillLable("yue", this.txtMonthL.Value);
  19. wop.FillLable("ri", this.txtDaysL.Value);
  20. wop.SaveAs(path + "VATInvoiceDocument1.doc", true); //将要保存到的Word文档
  21. wop.Quit();
  22. Response.Redirect(@"/UploadFiles/VATInvoiceDocument1.doc"); //做个跳转用于下载.
  23. }

WordOp类的代码实现:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using Microsoft.Office.Interop.Word;
  11. using System.IO;
  12.  
  13. namespace CCIR.CorpWebSite.WebPage
  14. {
  15. public class WordOp
  16. {
  17. public WordOp()
  18. {
  19. //
  20. // TODO: 在此处添加构造函数逻辑
  21. //
  22. }
  23. private ApplicationClass WordApp;
  24. private Document WordDoc;
  25. private static bool isOpened = false;//判断word模版是否被占用
  26. public void SaveAs(string strFileName, bool isReplace)
  27. {
  28. if (isReplace && File.Exists(strFileName))
  29. {
  30. File.Delete(strFileName);
  31. }
  32. object missing = Type.Missing;
  33. object fileName = strFileName;
  34. WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
  35. ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  36. }
  37. //定义一个Word.Application 对象
  38. public void activeWordApp()
  39. {
  40. WordApp = new ApplicationClass();
  41. }
  42. public void Quit()
  43. {
  44. object missing = System.Reflection.Missing.Value;
  45. WordApp.Application.Quit(ref missing, ref missing, ref missing);
  46. isOpened = false;
  47. }
  48. //基于模版新建Word文件
  49. public void OpenTempelte(string strTemppath)
  50. {
  51. object Missing = Type.Missing;
  52. //object Missing = System.Reflection.Missing.Value;
  53. activeWordApp();
  54. WordApp.Visible = false;
  55. object oTemplate = (object)strTemppath;
  56. try
  57. {
  58. while (isOpened)
  59. {
  60. System.Threading.Thread.Sleep();
  61. }
  62. WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
  63. isOpened = true;
  64. WordDoc.Activate();
  65. }
  66. catch (Exception Ex)
  67. {
  68. Quit();
  69. isOpened = false;
  70. throw new Exception(Ex.Message);
  71. }
  72. }
  73. public void FillLable(string LabelId, string Content)
  74. {
  75. //打开Word模版
  76. // OpenTempelte(tempName); //对LabelId的标签进行填充内容Content,即函件题目项
  77. object bkmC = LabelId;
  78. if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
  79. {
  80. WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
  81. }
  82. WordApp.Selection.TypeText(Content);
  83. //SaveAs(saveAsFileName);
  84. //Quit();
  85. }
  86. }
  87. }

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

  

.NET 动态向Word文档添加数据的更多相关文章

  1. C#动态生成Word文档并填充数据

    C#也能动态生成Word文档并填充数据 http://www.cnblogs.com/qyfan82/archive/2007/09/14/893293.html 引用http://blog.csdn ...

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

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

  3. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  4. 使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案

    使用C#动态生成Word文档/Excel文档的程序功能调试.测试通过后,部署到服务器上,不能正常使用的问题解决方案: 原因: 可能asp.net程序或iis访问excel组件时权限不够(Ps:Syst ...

  5. 向Docx4j生成的word文档添加图片和布局--第一部分

    原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...

  6. [转载]Java动态填充word文档并上传到服务器

    一. 需求背景 在一些特殊应用场合,客户希望在服务器上生成文档的同时并填充数据,客户端的页面不显示打开文档,但是服务器上生成文档对服务器压力很大,目前服务器上生成文档第一种就是方式是jacob, 但是 ...

  7. [原创]Java动态填充word文档并上传到服务器

    一. 需求背景 在一些特殊应用场合,客户希望在服务器上生成文档的同时并填充数据,客户端的页面不显示打开文档,但是服务器上生成文档对服务器压力很大,目前服务器上生成文档第一种就是方式是jacob, 但是 ...

  8. C# 给Word文档添加内容控件

    C# 给Word文档添加内容控件 在MS Word中,我们可以通过内容控件来向word文档中插入预先定义好的模块,指定模块的内容格式(如图片.日期.列表或格式化的文本等),从而创建一个结构化的word ...

  9. OpenXml入门----给Word文档添加文字

    使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...

随机推荐

  1. RDD认知

    1.RDD又叫弹性分布式数据集 2.抽象 3.带泛型,支持多种数据类型 4.集合是可以进行分区 例如(1,2,3,4,5,6,7,8,9)这个数组是可以进行分区的(1,2,3)  (4,5,6)  ( ...

  2. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  3. cisco PBR

    access-list 2000 permit ip 10.11.50.0 0.0.0.255 anyaccess-list 2001 permit ip 10.11.50.0 0.0.0.255 1 ...

  4. 在java项目中使用umeditor

    之前有介绍了ueditor的用法,可看这篇:https://www.cnblogs.com/roy-blog/p/7250668.html umeditor是ueditor的简化版,不仅在功能,容量上 ...

  5. EntityFramework 基础提供程序在 Open 上失败

    最近项目开始上线,所以抽时间学习了一下EF.虽然项目中一直在用,但是因为一些原因,一直是知其然不知其所以然,紧紧只限于会用而已.这两天自己搭建了一个MVC的EF框架,虽然也有参考网上各种资料,但是依然 ...

  6. Quartz.Net进阶之二:关于触发器的更多信息

    与作业一样,触发器相对容易使用,但是在您可以充分利用Quartz.NET之前,确实需要了解和理解各种可自定义的选项. 此外,如前所述,您可以选择不同类型的触发器来满足不同的调度需求. 1.常见触发器属 ...

  7. Python开发——数据类型【字符串】

    字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...

  8. Json中对日期的处理

    前言:Json对日期的处理很特别,我们不能简单的转换而得到我们想要的结果,需要进行特殊处理 一.JSon序列化和反序列化对日期的处理 JsonHelper类: using System.IO; usi ...

  9. selenium+java+chrome 自动化测试环境搭建

    安装jdk    (jdk 配置环境变量)    eclipse(可用免安装的) 安装谷歌浏览器 下载chorme driver (chorme driver 也要配置环境变量,将chormedriv ...

  10. JavaScript:void(0)使用介绍

    1.点击链接后不做任何事情(为防止点击链接后跳转到页首,onclick事件return false即可) <a href="javascript:void(0);" > ...