C#查找并在Word文档中替换文本 [英] c# Find and Replace Text in a Word Document
本文介绍了C#查找并在Word文档中替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建和操纵Word文档以编程方式使用DOCX!并创建一个Word模板 InvoiceTemplate.docx
这是我的code:
i want to Create and Manipulate Word Documents Programmatically Using DocX ! and create a word template InvoiceTemplate.docx
this is my code :
protected void CreateDoc_Click(object sender, EventArgs e)
{
//Createa docx File
DocX gDocument;
gDocument =
DocX.Load(@Server.MapPath("InvoiceTemplate.docx"));
gDocument =
CreateInvoiceFromTemplate(DocX.Load(@Server.MapPath("InvoiceTemplate.docx")));
gDocument.SaveAs(@Server.MapPath("~/DocX/NewLoadedShipment.docx"));
}
private static DocX CreateInvoiceFromTemplate(DocX template)
{
template.AddCustomProperty(
new CustomProperty("{rechnung}", "5"));
template.AddCustomProperty(
new CustomProperty("{name}", "Maziar"));
template.AddCustomProperty(
new CustomProperty("{date}", DateTime.Now.ToString("dd.MM.yyyy")));
template.AddCustomProperty(
new CustomProperty("{address}", "12345hamburg"));
template.AddCustomProperty(
new CustomProperty("{tell}", "2234543"));
template.AddCustomProperty(
new CustomProperty("{amount}", "1000"));
template.AddCustomProperty(
new CustomProperty("{rest}", "500"));
template.AddCustomProperty(
new CustomProperty("{tax}", "100"));
template.AddCustomProperty(
new CustomProperty("{total}", "600"));
return template;
}
但没有happend我的 NewLoadedShipment.docx
!能有人帮助,请!
推荐答案
所以我通常提倡使用DocXTemplateEngine用于这一目的。比使用新星code-DOCX模板更换简单。
So I usually advocate Using DocXTemplateEngine for this purpose. Simpler than using novacode-docx for template replacement.
填写标准的邮件合并域以简单的方式
Fills in standard mail merge fields in a simple way
var templateEngine = new swxben.docxtemplateengine.DocXTemplateEngine();
templateEngine.Process(
source = "template.docx",
destination = "dest.docx",
data = new {
Name = "SWXBEN"
}
);
https://www.nuget.org/packages/swxben.docxtemplateengine/一>
这篇关于C#查找并在Word文档中替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文