首页 新闻 赞助 找找看

asp.net 中调用Aspose.Words 导出到word模板,数据一直出不来很奇怪???同样的代码再控制台可以,Controller里不行

0
悬赏园豆:10 [待解决问题]

string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+ "bin\Template.doc";
Document doc = new Document(path);
DocumentBuilder builder = new DocumentBuilder(doc);
var dic = new Dictionary<string, string>();
dic.Add("今日", "100");
foreach (var key in dic.Keys)
{
if (key != "今日")
{
var repStr = string.Format("&{0}&", key);
doc.Range.Replace(repStr, dic[key], false, false);
}

        }
        
        doc.Save("Template.doc", SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

上面的方法我是写在了Controller文件里面 然后再cshtml页面中去调用。。。
我同样的代码在控制台写了一遍就可以 代码如下就能替换 word模板中 &今日& 里面的值

var dic = new Dictionary<string, string>();
dic.Add("今日", "100");
dic.Add("全部", "999");
Document doc = new Document(".//Template.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

        //使用特殊字符串替换
        doc = new Document(".//Template.doc");
        foreach (var key in dic.Keys)
        {
            if (key == "今日")
            {
                var repStr = string.Format("&{0}&", key);
                doc.Range.Replace(repStr, dic[key], false, false);
            }
           
        }
        doc.Save("Template.doc");//也可以保存为1.doc 兼容03-07
        Console.WriteLine("已经完成特殊字符串替换操作");

请大家指点,赐教

< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册