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("已经完成特殊字符串替换操作");
请大家指点,赐教