C#中的Openxml仅更新段落中的第一个MERGEFIELD

时间:2016-07-25 21:25:01

标签: c# merge openxml

我在文档中大约有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个合并字段,之后每个段落中有一个字段。只更新段落中的第一个合并字段,并且段落中的其余字段不受影响。然后,它移动到下一段并查找该字段。我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

看起来你已经过度复杂了一个简单的Mailmerge替代品。

代替循环段落,您可以更好地获取文档中的所有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。

无论段落中的字段数量如何,这都将替换所有合并字段。