【问题标题】:Openxml in C# updating only the first MERGEFIELD in a paragraphC# 中的 Openxml 仅更新段落中的第一个 MERGEFIELD
【发布时间】:2016-07-25 21:25:01
【问题描述】:

我在一个文档中有大约 10 个MERGEFIELD,我试图用一些值替换 Text。这是代码。

using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFileName, true))
            {
                document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                MainDocumentPart docPart = document.MainDocumentPart;
                docPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(destinationFileName, UriKind.RelativeOrAbsolute));
                docPart.Document.Save();

                IEnumerable<FieldChar> fldChars = document.MainDocumentPart.RootElement.Descendants<FieldChar>();
                if (fldChars == null) { return; }

                string fieldList = string.Empty;
                FieldChar fldCharStart = null;
                FieldChar fldCharEnd = null;
                FieldChar fldCharSep = null;
                FieldCode fldCode = null;
                string fldContent = String.Empty;
                int i = 1;
                foreach(var fldChar in fldChars)
                {

                    System.Diagnostics.Debug.WriteLine(i + ": " + fldChar);
                    i++;

                    string fldCharPart = fldChar.FieldCharType.ToString();
                    System.Diagnostics.Debug.WriteLine("Field Char Length: " + fldChar.Count());
                    System.Diagnostics.Debug.WriteLine("Field Char part: " + fldCharPart);

                    switch(fldCharPart)
                    {    
                        case "begin": // start of the field
                            fldCharStart = fldChar;
                            System.Diagnostics.Debug.WriteLine("Field Char Start: " + fldCharStart);
                            // get the field code, which will be an instrText element
                            // either as sibling or as a child of the parent sibling

                            fldCode = fldCharStart.Parent.Descendants<FieldCode>().FirstOrDefault();
                            System.Diagnostics.Debug.WriteLine("Field Code: " + fldCode);

                            if (fldCode == null)
                            {
                                fldCode = fldCharStart.Parent.NextSibling<Run>().Descendants<FieldCode>().FirstOrDefault();
                                System.Diagnostics.Debug.WriteLine("New Field Code: " + fldCode);
                            }

                            if (fldCode != null && fldCode.InnerText.Contains("MERGEFIELD"))
                            {
                                fldContent = getFieldValue(query, prescriber, fldCode.InnerText);
                                fieldList += fldContent + "\n";
                                System.Diagnostics.Debug.WriteLine("Field content: " + fldContent);

                            }
                            break;

                        case "end": // end of the field
                            fldCharEnd = fldChar;
                            System.Diagnostics.Debug.WriteLine("Field char end: " + fldCharEnd);
                            break;

                        case "separate": // complex field with text result
                            fldCharSep = fldChar;
                            break;

                        default:
                            break;  

                    }

                    if((fldCharStart != null) && (fldCharEnd != null))
                    {
                        if(fldCharSep != null)
                        {
                            Text elemText = (Text)fldCharSep.Parent.NextSibling().Descendants<Text>().FirstOrDefault();
                            elemText.Text = fldContent;
                            System.Diagnostics.Debug.WriteLine("Element text: " + elemText);

                            // Delete all field chas with their runs
                            DeleteFieldChar(fldCharStart);
                            DeleteFieldChar(fldCharEnd);
                            DeleteFieldChar(fldCharSep);
                            fldCode.Remove();
                        }
                        else
                        {
                            Text elemText = new Text(fldContent);
                            fldCode.Parent.Append(elemText);
                            fldCode.Remove();
                            System.Diagnostics.Debug.WriteLine("Element Text !sep: " + elemText);

                            DeleteFieldChar(fldCharStart);
                            DeleteFieldChar(fldCharEnd);
                            DeleteFieldChar(fldCharSep);
                        }

                        fldCharStart = null;
                        fldCharEnd = null;
                        fldCharSep = null;
                        fldCode = null;
                        fldContent = string.Empty;
                    }
                }
                System.Diagnostics.Debug.WriteLine("Field list: " + fieldList);
            }

它在某种程度上有效。问题是当段落中有多个字段时。我在本文档的一个段落中有大约 4 个合并字段,之后的每个段落中有一个字段。只有段落中的第一个合并字段正在更新,段落中的其余字段保持不变。然后,它移动到下一段并查找该字段。我怎样才能解决这个问题?

【问题讨论】:

    标签: c# merge openxml


    【解决方案1】:

    看起来您对简单的 Mailmerge 替换过于复杂了。与其遍历段落,不如获取文档中的所有邮件合并字段并替换它们。

        private const string FieldDelimeter = " MERGEFIELD ";
    
        foreach (FieldCode field in doc.MainDocumentPart.RootElement.Descendants<FieldCode>())
        {
             var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);
             var fieldName = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();
    
             foreach (Run run in doc.MainDocumentPart.Document.Descendants<Run>())
             {
                   foreach (Text txtFromRun in run.Descendants<Text>().Where(a => a.Text == "«" + fieldName + "»"))
                   {                            
                         txtFromRun.Text = "Replace what the merge field here";
                   }
             }
         } 
    
    
        doc.MainDocumentPart.Document.Save();
    

    doc 是 WordprocessingDocument 类型。

    无论段落中有多少字段,这将替换所有合并字段。

    【讨论】:

    • 这不起作用。它不会用值替换字段。我的 fieldName 显示为fieldName \* MERGEFORMAT,这可能与它有关吗?但即使我用\* MERGEFORMAT 替换了“>”,它仍然不会进入第二个foreach 循环。有什么建议吗?
    • 这是正确答案。我在提取字段名称时犯了一个错误。我没有做 Trim() 导致字段名后的空格与文本不匹配。即使在执行了 Trim() 和调试之后,我仍然遇到错误,因为我显然已经在修剪后的数据上再次完成了子字符串。它现在正在工作。谢谢你教我怎么做。我觉得我现在更了解它了。
    • 如何处理MERGEFIELD中的“fldSimple”?我有使用 MERGEFIELD 生成超链接的情况。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2015-08-29
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多