C# 操作word 模板 值 替换

1.引用 aspose.words   dll

2.word 使用doc  

3.给word 模板中添加要替换位置的 书签

1.引用 aspose.words   dll
2.word 使用doc  

3.给word 模板中添加要替换位置的 书签



 string templatePath = Server.MapPath("~" + path);//文档模板物理路径

            Document doc = new Document(templatePath);
            try
            {
                Hashtable tables = new Hashtable();
                tables.Add("协议编号", ""+agreeNum+"");
                tables.Add("基金管理人", orgName);
                tables.Add("基金管理人住所", orgFundCompanyAddr);
                tables.Add("法定代表人", orgCorporationName);
                tables.Add("基金投资者", investorName);
                tables.Add("基金投资者住所", InvestorAddress);
                tables.Add("投资者身份证号", investorIdCard);
                tables.Add("投资者性别", InvestorSex);
                tables.Add("投资者联系方式", InvestorTel);
                tables.Add("投资者年龄", InvestorAge);
                

                tables.Add("基金合同编号", ""+htNum+"");
                tables.Add("基金合同编号2",  htNum );
                tables.Add("基金合同名称", ""+fundName+"私募基金合同》");
                tables.Add("金额大写", Common.Utils.MoneyToUpper((Utils.StrToDecimal(loanAmount, 0)).ToString()).Replace("圆整",""));//不带单位只读数字

                tables.Add("金额小写", (String.Format("{0:N}", Utils.StrToDecimal(loanAmount, 0) * 10000)));
                tables.Add("金额小写不乘一万", (String.Format("{0:N}", Utils.StrToDecimal(loanAmount, 0))));
                tables.Add("付款日期", beginTime);
                tables.Add("付款日期2", beginTime2);
                tables.Add("收益开始日期", beginTime);
                tables.Add("封闭开始日期", beginTime);
                tables.Add("封闭结束日期", endTime);
                tables.Add("封闭月数", totalMonth);
                tables.Add("开户行", fundActBankName);
                tables.Add("银行卡号", fundActBankNum);
                tables.Add("基金账户名称", fundActName);
                tables.Add("通知日期", beginTime);
                tables.Add("基金名称", fundName + "私募投资");
                tables.Add("委托人银行卡号", agentBankNum);
                tables.Add("委托人开户行", agentBankName);
                tables.Add("委托人账户名称", agentName);

                GetHTFile(doc, tables);
                string downname = investorName + "-" + name;
                doc.Save(downname, SaveFormat.Doc, SaveType.OpenInWord, Page.Response);
            }
            catch(Exception ex)
            {
               
            }
  
public static void GetHTFile(Document doc, Hashtable table)
        {
            BookmarkCollection bookmarks = doc.Range.Bookmarks;
            foreach(Bookmark mark in bookmarks)
            {
                if(table.ContainsKey(mark.Name))
                {
                    mark.Text = table[mark.Name].ToString();
                }
            }
        }

  

原文地址:https://www.cnblogs.com/kaikaichao/p/10270175.html