【发布时间】:2019-03-31 05:40:51
【问题描述】:
如何通过OpenXML在MS Word文档中设置{DOCVARIABLE SomeName \* MERGEFORMAT}等字段的值?
【问题讨论】:
-
该解决方案通过 Microsoft.Office.Interop。我没有在计算机上安装 MS Word(这是服务器)。
标签: c# asp.net-mvc openxml docx docvariable
如何通过OpenXML在MS Word文档中设置{DOCVARIABLE SomeName \* MERGEFORMAT}等字段的值?
【问题讨论】:
标签: c# asp.net-mvc openxml docx docvariable
文档变量存储为DocumentVariables 元素内的DocumentVariable 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();
}
}
}
}
【讨论】: