C# 如何将标题应用于字处理文档中的所有段落?
有没有一种方法可以循环遍历文档中的段落元素? 到目前为止,我还可以选择单独的段落来应用标题。但是我想循环所有这些,或者计算()我有多少,这样我就可以使用for循环C# 如何将标题应用于字处理文档中的所有段落?,c#,xml,openxml,docx,C#,Xml,Openxml,Docx,有没有一种方法可以循环遍历文档中的段落元素? 到目前为止,我还可以选择单独的段落来应用标题。但是我想循环所有这些,或者计算()我有多少,这样我就可以使用for循环 using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true)) { int i = 0; Paragraph p = doc.MainDocumentPart.Document.Body.Descendants<Pa
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);
}
使用(WordprocessingDocument=WordprocessingDocument.Open(fileName,true))
{
int i=0;
第p段=doc.MainDocumentPart.Document.Body.Subjections().ElementAt(i);
//检查空引用。
if(p==null)
{
抛出新ArgumentOutOfRangeException(“p”,“未找到段落”);
}
ApplyStyleToParagraph(文件“标题1”、“标题1”、p);
}
你就快到了
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
}
}
}
使用(WordprocessingDocument=WordprocessingDocument.Open(fileName,true))
{
var段落=doc.MainDocumentPart.Document.Body.subjects().ToList();
foreach(第段中的var段)
{
if(para==null)
{
//抛出异常
}
其他的
{
//应用样式
}
}
}
你就快到了
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
}
}
}
使用(WordprocessingDocument=WordprocessingDocument.Open(fileName,true))
{
var段落=doc.MainDocumentPart.Document.Body.subjects().ToList();
foreach(第段中的var段)
{
if(para==null)
{
//抛出异常
}
其他的
{
//应用样式
}
}
}
这似乎比微软提供的要简单得多!谢谢萨克!为了进一步说明这一点,我如何检查这些段落是否有特定的文本?我试过if(para.Contains(“apple”)),但遇到了错误。这似乎比微软的测试要容易得多!谢谢萨克!为了进一步说明这一点,我如何检查这些段落是否有特定的文本?我曾经尝试过if(段落包含(“apple”)),但遇到了错误。