0

如何{DOCVARIABLE SomeName \* MERGEFORMAT}通过 OpenXML 在 MS Word 文档等字段中设置值?

4

1 回答 1

0

文档变量存储为DocumentVariables元素中的DocumentVariable ,位于DocumentSettingsPartSettings元素中

因此,您可以按如下方式检索 Documentvariable:

    /// <summary>
    /// Retrieves a document variable by its name
    /// </summary>
    /// <param name="name">The name of the document variable (case sensitive)</param>
    /// <param name="document">The wordprocessing document</param>
    /// <returns>The document variable if found, other wise it returns null</returns>
    public DocumentVariable GetVariableByName(string name, WordprocessingDocument document)
    {
        // Get the document settings part
        DocumentSettingsPart documentSettings = document.MainDocumentPart.DocumentSettingsPart;
        // Get the settings element
        Settings settings = documentSettings.Settings;
        // Get the DocumentVariables element
        DocumentVariables variables = settings.Elements<DocumentVariables>().FirstOrDefault();
        // check if the variables are not null
        if(variables != null)
        {
            return variables.Elements<DocumentVariable>().Where(v => v.Name == name)
                .FirstOrDefault();
        }
        return null;
    }

检索变量时,您可以按如下方式更改变量的值:

    /// <summary>
    /// Sets the value of a document variable
    /// </summary>
    /// <param name="variable">The variable</param>
    /// <param name="value">The value</param>
    public void SetDocumentVariableValue(DocumentVariable variable, string value)
    {
        variable.Val = value;
    }

将所有这些放在一个简单的控制台程序中,WordProcessing 文档中的文档变量的值可以更改如下:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Linq;

namespace ChangeDocVariable
{
    class Program
    {
        /// <summary>
        /// Retrieves a document variable by its name
        /// </summary>
        /// <param name="name">The name of the document variable (case sensitive)</param>
        /// <param name="document">The wordprocessing document</param>
        /// <returns>The document variable if found, other wise it returns null</returns>
        public static DocumentVariable GetVariableByName(string name, WordprocessingDocument document)
        {
            // Get the document settings part
            DocumentSettingsPart documentSettings = document.MainDocumentPart.DocumentSettingsPart;
            // Get the settings element
            Settings settings = documentSettings.Settings;
            // Get the DocumentVariables element
            DocumentVariables variables = settings.Elements<DocumentVariables>().FirstOrDefault();
            // check if the variables are not null
            if(variables != null)
            {
                return variables.Elements<DocumentVariable>().Where(v => v.Name == name)
                    .FirstOrDefault();
            }
            return null;
        }

        /// <summary>
        /// Sets the value of a document variable
        /// </summary>
        /// <param name="variable">The variable</param>
        /// <param name="value">The value</param>
        public static void SetDocumentVariableValue(DocumentVariable variable, string value)
        {
            variable.Val = value;
        }

        static void Main(string[] args)
        {
            string path = @"This should be the path to your document";

            using(WordprocessingDocument document = WordprocessingDocument.Open(path, true))
            {
                DocumentVariable variable = GetVariableByName("TestVariable", document);
                if(variable != null)
                    SetDocumentVariableValue(variable, "New Value");
                // Or access the value directly
                // variable.Val = "New Value";
                document.Save();
            }
        }
    }
}
于 2019-04-17T08:57:51.647 回答