如何将内容放入docx的DOCVARIABLE字段中

时间:2019-03-31 05:40:52

标签: c# asp.net-mvc openxml docx docvariable

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

1 个答案:

答案 0 :(得分:0)

文档变量在DocumentVariableDocumentVariables元素内的Settings元素内以DocumentSettingsPart的形式存储

因此,您可以按以下方式检索Document变量:

    /// <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();
            }
        }
    }
}