根据word模板导出,替换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xyxx_base.Models;
using istrong.db;
using Aspose.Words;
using System.IO;
using System.Configuration;

namespace xyxx_base.Common
{
    public static class WordHelper
    {
        private readonly static DbHelper db = DbHelperFactory.GetDbHelper();

        public static string GetBidReport(TenderProject tp)
        {
            //专家名单
            string experts = "";
            foreach (var expert in tp.Experts)
            {
                if (expert.IsLeader)
                {
                    experts += expert.Name + "(主任)、";
                }
                else
                {
                    experts += expert.Name + "";
                }
            }
            experts = experts.Remove(experts.Count() - 1);//删除最后一个断号
            //获取中标候选人
            var tss = db.LinqQuery<TenderSignup>().Where(e => e.TpId == tp.Id && e.BidderOrder > 0).OrderBy(e => e.BidderOrder).ToList();
            var firstEntp = tss[0].UserEntpInfo.UserEntpName;
            var secondEntp = tss[1].UserEntpInfo.UserEntpName;
            var thirdEntp = tss[2].UserEntpInfo.UserEntpName;

            //获取总报名数,无效企业数
            var entpTotalCount = db.LinqQuery<TenderSignup>().Where(e => e.TpId == tp.Id && !e.IsRevoke).Count();
            var entpInvalidCount = db.LinqQuery<TenderSignup>().Where(e => e.TpId == tp.Id && !e.IsRevoke && (!e.IsEnter || !e.IsDecry || !e.IsSignup)).Count();

            //构成需要替换的字典
            var dict = new Dictionary<string, string>();
            dict["招标标题"] = tp.Title;
            dict["招标编号"] = tp.Detail.GetStringValue("招标编号");
            dict["招标人"] = tp.Detail.GetStringValue("招标人");
            dict["当前时间"] = DateTime.Now.ToString("yyyy年MM月dd日");
            dict["设备造价"] = tp.Detail.GetStringValue("设备造价");
            dict["地区"] = "福州市";
            dict["招标方式"] = tp.Detail.GetStringValue("招标方式");
            dict["交货期"] = tp.Detail.GetStringValue("交货期");
            dict["专家名单"] = experts;
            dict["总报名数"] = entpTotalCount.ToString();
            dict["无效企业"] = entpInvalidCount.ToString();
            dict["第一候选人"] = firstEntp;
            dict["第二候选人"] = secondEntp;
            dict["第三候选人"] = thirdEntp;

            string baseWay = System.AppDomain.CurrentDomain.BaseDirectory;
            Document doc = new Document(baseWay + "fjebid\template\common\评标报告模板.doc");
            foreach (var key in dict.Keys)
            {
                var repStr = string.Format("&{0}&", key);
                doc.Range.Replace(repStr, dict[key], true, false);
            }

            //保存
            string fileName = tp.Title + "评标报告" + ".doc";

            string currentTime = DateTime.Now.ToString("yyyy-MM");
            string fileWay = baseWay + "files\" + currentTime;
            if (!System.IO.File.Exists(fileWay))
            {
                Directory.CreateDirectory(fileWay);
            }

            doc.Save(fileWay + "\" + fileName);

            return ConfigurationManager.AppSettings["Domain"] + "/files/" + currentTime + "/" + fileName;

        }
    }
}
原文地址:https://www.cnblogs.com/ysf123/p/4380286.html