使用页眉,页脚和表格创建OpenXML Word文档

时间:2018-11-17 20:48:33

标签: vb.net openxml

在四处搜寻之后,我放弃了并寻求帮助。我正在尝试使用OpenXML创建一个Word文档,该文档的页眉和页脚中有一个图像,并且其中有一个表。我可以设置文档格式,创建表格,应用文本等,但是无法将任何内容放入标题中。到目前为止,我找到的帮助要么显示了如何应用其他文档的标头,要么似乎不适用于我构建文档的方式。我真的很感谢您的帮助。我可能只是想念一些简单的东西,但是我很困惑。

我所需要的只是创建一个文档的示例,该文档的页眉和页脚具有图像,主体具有表格。

谢谢

公共子CreateWordprocessingDocument(ByVal文件路径为字符串)

    Dim wordDocument As WordprocessingDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)
    Using wordDocument
        ' Add a main document part. 
        Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()



        Dim headerPart As HeaderPart = mainPart.AddNewPart(Of HeaderPart)()




        ' Create the document structure and add some text.
        mainPart.Document = New Document()

        Dim headerFooter As Header = mainPart.Document.AppendChild(New Header)
        Dim headerRun As New Run
        Dim headerText As New Text
        Dim headerParagraph As New Paragraph
        Dim header As New Header
        headerText.Text = "burp"
        headerRun.Append(headerText)
        headerParagraph.Append(headerRun)
        header.Append(headerParagraph)
        headerFooter.Append(header)



        'code to support orientation And margins
        Dim sectProp As SectionProperties = New SectionProperties()
        Dim PageSize As PageSize = New PageSize() With {.Width = 16838, .Height = 11906, .Orient = PageOrientationValues.Landscape}
        Dim PageMargin As PageMargin = New PageMargin() With {.Top = 720, .Right = 720, .Bottom = 720, .Left = 720}

        sectProp.Append(PageSize)
        sectProp.Append(PageMargin)


        Dim body As Body = mainPart.Document.AppendChild(New Body())

        ' Create an empty table.
        body.Append(sectProp)
        Dim table As New Table()

        ' Create a TableProperties object and specify its border information.
        'This sets up the table properties. Any cells added subsequently will follow these properties
        Dim tblProp As New TableProperties(New TableBorders(
        New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5}, '.size is the weight of the line. 10 = 1 point thick line
        New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5},
        New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5},
        New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5},
        New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5}, 'internal borders within a table
        New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.BasicThinLines), .Size = 5}))
        ' Append the TableProperties object to the empty table.
        table.AppendChild(Of TableProperties)(tblProp)


        'Build the table dynamically from top left to bottom right adding in properties as we go

        '#################
        '#CREATING TABLE #
        '#################

        'Specify the width property of all the cells at the top of the sheet.
        'first row


        Dim row1 As New TableRow()
        row1.Append(createcell("Grey Row", 1500, "CCCCCC"))
        row1.Append(createcellMerge("", 1500))

        table.Append(row1)


        '#####adding an image to a cell - this works. Need to work on the positioning once I have the size of the cell
        Dim imagePart As ImagePart = mainPart.AddImagePart(ImagePartType.Jpeg)

        Using stream As New FileStream(pictureName, FileMode.Open)
            imagePart.FeedData(stream)
        End Using

        'AddImageToCell(tc1, mainPart.GetIdOfPart(imagePart))
        body.Append(table)

    End Using

    Process.Start(filepath)



End Sub

0 个答案:

没有答案