zoukankan      html  css  js  c++  java
  • .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(); 
            }
        }
    }

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

      

  • 相关阅读:
    iOS-开发日志-UIButton
    苹果API常用英语名词
    iOS-开发日志-UIimageView
    IOS-开发日志-UILabel相关
    iOS-开发日志-UIPageControl
    Maven-生成可执行的Jar包
    RabbitMQ不讲武德,发个消息也这么多花招
    Azure Service Bus(三)在 .NET Core Web 应用程序发送ServiceBus Queue
    windows server 2012 R2里IIS配置.net core2.1遇到的坑
    VScode中配置C++运行环境
  • 原文地址:https://www.cnblogs.com/swjian/p/6811532.html
Copyright © 2011-2022 走看看