Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将标题应用于字处理文档中的所有段落?_C#_Xml_Openxml_Docx - Fatal编程技术网

C# 如何将标题应用于字处理文档中的所有段落?

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

有没有一种方法可以循环遍历文档中的段落元素? 到目前为止,我还可以选择单独的段落来应用标题。但是我想循环所有这些,或者计算()我有多少,这样我就可以使用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);
}
使用(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”)),但遇到了错误。