Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#查找并替换Word文档中的文本_C#_Asp.net_Ms Word_Docx_Novacode Docx - Fatal编程技术网

c#查找并替换Word文档中的文本

c#查找并替换Word文档中的文本,c#,asp.net,ms-word,docx,novacode-docx,C#,Asp.net,Ms Word,Docx,Novacode Docx,我想使用DocX以编程方式创建和操作Word文档!并创建一个word模板InvoiceTemplate.docx这是我的代码: protected void CreateDoc_Click(object sender, EventArgs e) { //Createa docx File DocX gDocument; gDocument = DocX.Load(@Server.MapPath("InvoiceTemplate.docx"));

我想使用DocX以编程方式创建和操作Word文档!并创建一个word模板
InvoiceTemplate.docx
这是我的代码:

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;
}

但是我的
newloadedshipping.docx没有发生任何事情!有人能帮忙吗

因此,我通常主张为此使用DocXTemplateEngine。比使用novacode docx替换模板更简单

以简单的方式填写标准邮件合并字段

var templateEngine = new swxben.docxtemplateengine.DocXTemplateEngine();
templateEngine.Process(
    source = "template.docx",
    destination = "dest.docx",
    data = new {
       Name = "SWXBEN"
    }
); 

可用

DocX中有一个名为“ReplaceText”的函数,允许您替换文档中的文本

您必须首先引用您的模板,例如

DocX letter = DocX.Load(%YOUR TEMPLATE NAME%);
然后你可以做

letter.ReplaceText("%REPLACE_THIS%","%WITH_THIS%");

希望这有帮助

我们如何使用DocX>ReplaceText()方法仅替换第一次出现的内容。如果还有什么需要帮助的话。很紧急。先谢谢你