有没有一种方法可以遍历文档中的段落元素?到目前为止,我也可以选择单独的段落来应用标题。但我希望循环遍历所有这些元素,或者count()我有多少个元素,这样我就可以使用for循环。
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
{
int i = 0;
Paragraph p = doc.MainDocumentPart.Document.Body.Descendants<Paragraph>().ElementAt(i);
// Check for a null reference.
if (p == null)
{
throw new ArgumentOutOfRangeException("p", "Paragraph was not found.");
}
ApplyStyleToParagraph(doc, "Heading1", "Heading 1", p);
}
发布于 2018-08-10 03:29:36
你就快到了。
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
{
var paragraphs = doc.MainDocumentPart.Document.Body.Descendants<Paragraph>().ToList();
foreach (var para in paragraphs)
{
if (para == null)
{
// Throw exception
}
else
{
// Apply style
}
}
}
https://stackoverflow.com/questions/51774377
复制相似问题