1
我想在文档的标题中替换占位符'plcDate'。标题从我的文档模板中的第二页开始。替换标题占位符,现在不工作
我正在使用下面的代码。但'headDate'始终为空。
不知道我是否需要更改代码或文档模板。
using (WordprocessingDocument theDoc = WordprocessingDocument.Open(NewPath, true))
{
MainDocumentPart mainPart = theDoc.MainDocumentPart;
foreach (HeaderPart hpart in mainPart.HeaderParts)
{
SdtElement headDate = hpart.Header.Descendants<SdtElement>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "plcDate").SingleOrDefault();
if (headDate != null)
{
headDate.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(new Text(dateValue))));
}
}
mainPart.Document.Save();
}
在'foreach'是一个错字,一旦你有'headDate'和两次'headSDate'? – Philippe
对不起。这是一个错字。谢谢 – Ashin