【问题标题】:OpenXML asp.net loop through headerOpenXML asp.net 循环通过标头
【发布时间】:2013-10-15 20:29:03
【问题描述】:

我有一个 10 页的文档。每个页面都有页眉和页脚。标题可能有两行或一行文本,每一行都有不同的样式。想要做的是循环浏览文档。阅读每页的页眉和页脚,将其放入DataTable,以便我稍后构建目录。任何想法,我都试过了,但它不能正常工作,它没有读取每个页面页脚并跳过页面(这似乎是乱序的,我想按页面顺序获取值,因为它似乎跳过了第一页直到最后一页迭代)。

我们将不胜感激。 JT

 Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(combineDocName, True)
        For Each Head As HeaderPart In wordDoc.MainDocumentPart.HeaderParts
            For Each currentParagraph As DocumentFormat.OpenXml.Wordprocessing.Paragraph In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
          Dim p As ParagraphProperties = currentParagraph.Elements(Of ParagraphProperties)().First()
                If p.Count > 0 Then
                    If (p.ParagraphStyleId IsNot Nothing) Then
                        If p.ParagraphStyleId.Val.ToString() = "HeaderBar" Then
                            For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
                                If (String.IsNullOrEmpty(keepHeaderM)) Then
                                    HeaderBarTxt = currentText.Text.Trim()
                                ElseIf keepHeaderM <> currentText.Text.Trim() Then
                                    HeaderBarTxt = currentText.Text.Trim()
                                End If
                            Next
                        ElseIf p.ParagraphStyleId.Val.ToString() = "NavigationBar" Then
                            For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
                                iCount = currentText.Text.Split(":").Length - 1
                                If (String.IsNullOrEmpty(keepHeaderM)) Then
                                    HeaderTxt = currentText.Text.Trim()
                                ElseIf keepHeaderM <> currentText.Text.Trim() Then
                                    HeaderTxt = currentText.Text.Trim()
                                End If
                            Next
                        End If
                    End If
                End If
             Next
          Next
           For Each foot As FooterPart In wordDoc.MainDocumentPart.FooterParts
                For Each currentParagraph2 As DocumentFormat.OpenXml.Wordprocessing.Paragraph In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
                    If currentParagraph2.Count > 0 Then
                        For Each currentText2 As Text In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
                            Dim strTemp As String = currentText2.Text
                            If strTemp.IndexOf("-") <> -1 Then
                                FooterTxt = currentText2.Text.Trim()

                            End If
                        Next
                    End If

                Next
            Next
 end using  

【问题讨论】:

  • 究竟是什么不起作用?文件的格式是什么?
  • 我发现当你循环时它并没有按顺序执行我需要的是如何按顺序循环浏览页面......我尝试了以下但我仍然首先得到第 3 页由于某种原因,第一页最后一页。我如何按顺序循环播放?

标签: asp.net vb.net openxml


【解决方案1】:

如果您在 Word 文件中有不同的页眉和页脚,那么我假设您的 Word 文档中有多个部分。

您必须遍历这些部分,然后读取这些部分中的页脚和页眉,然后将它们保存在 DataTable 中的某个位置。

关于创建目录的问题。

您可以使用手动创建的 TOC 创建一个单独的 Word 文档,例如 TOC.docx。 现在,当您想要添加 TOC 时,您创建 TOC.docx 的副本,然后将此文件与您想要添加 TOC 的文档合并。 不要忘记在合并文档的设置中将自动更新设置为true。

<w:updateFields w:val="true" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    相关资源
    最近更新 更多