中导出数据到WORD中

首先要添加引用一个MicroSoft Word 11.0 Object Library的一个引用。

然后在程序的最上面引用一下声明
Imports Microsoft.office.interop
接着在相对应的按钮事件,添加如下代码即可,导出到Word中
                Dim oword As Word.Application
                Dim odoc As Word.Document
                Dim otable As Word.Table
                Dim opara1 As Word.Paragraph
                Dim opara2 As Word.Paragraph
                Dim opara3 As Word.Paragraph
                Dim opara4 As Word.Paragraph
                Dim orng As Word.Range
                Dim oshape As Word.InlineShape
                Dim ochart As Object
                Dim pos As Double

                '启动Microsoft Word并打开文件模板
                oword = CreateObject("Word.application")
                oword.Visible = True
                odoc = oword.Documents.Add

                '在文件的开头处插入一个段落
                opara1 = odoc.Content.Paragraphs.Add
                opara1.Range.Text = "标题一"
                opara1.Range.Font.Bold = True
                opara1.Format.SpaceAfter = 24 '在段落之后24 Pt 空格
                opara1.Range.InsertParagraphAfter()

                '在文件的尾端插入一个段落
                '**/endoFdoc 是一个事先定义的书签
                opara2 = odoc.Content.Paragraphs.Add(odoc.Bookmarks.Item("/endoFdoc").Range)
                opara2.Range.Text = "标题二"
                opara2.Format.SpaceAfter = 6
                opara2.Range.InsertParagraphAfter()

                '插入另外一个段落
                opara3 = odoc.Content.Paragraphs.Add(odoc.Bookmarks.Item("/endofdoc").Range)
                opara3.Range.Text = "这是单纯的一句文字。以下则是一个表格: "
                opara3.Range.Font.Bold = False
                opara3.Format.SpaceAfter = 24
                opara3.Range.InsertParagraphAfter()

                '插入一个 3 X 5 的表格,填入数据,然后
                '使第一行的表格成为粗体与协体。
                Dim r As Integer, c As Integer
                otable = odoc.Tables.Add(odoc.Bookmarks.Item("/endofdoc").Range, 3, 5)
                otable.Range.ParagraphFormat.SpaceAfter = 6
                For r = 1 To 3
                    For c = 1 To 5
                        otable.Cell(r, c).Range.Text = "行" & r & "栏" & c
                    Next
                Next
                otable.Rows.Item(1).Range.Font.Bold = True
                otable.Rows.Item(1).Range.Font.Italic = True

                '在表格之后添加一些文字
                'otable.Range.InsertParagraphAfter()
                opara4 = odoc.Content.Paragraphs.Add(odoc.Bookmarks.Item("/endofdoc").Range)
                opara4.Range.InsertParagraphBefore()
                opara4.Range.Text = "这里是另外一个表格:"
                opara4.Format.SpaceAfter = 24
                opara4.Range.InsertParagraphAfter()

                '插入一个5 X 2的表格,填入一些文字,并更改栏宽度。
                otable = odoc.Tables.Add(odoc.Bookmarks.Item("/endofdoc").Range, 5, 2)
                otable.Range.ParagraphFormat.SpaceAfter = 6
                For r = 1 To 5
                    For c = 1 To 2
                        otable.Cell(r, c).Range.Text = "行" & r & "栏" & c
                    Next
                Next
                '更改栏1与2的宽度。
                otable.Columns.Item(1).Width = oword.InchesToPoints(2)
                otable.Columns.Item(2).Width = oword.InchesToPoints(3)

                '继续插入文字。当您距离文字顶端7英寸
                '时,插入一个分页符。
                pos = oword.InchesToPoints(7)
                odoc.Bookmarks.Item("/endofdoc").Range.InsertParagraphAfter()
                Do
                    orng = odoc.Bookmarks.Item("/endofdoc").Range
                    orng.ParagraphFormat.SpaceAfter = 6
                    orng.InsertAfter("一行文字")
                    orng.InsertParagraphAfter()
                Loop While pos >= orng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
                orng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                orng.InsertBreak(Word.WdBreakType.wdPageBreak)
                orng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                orng.InsertAfter("我们现在位于第二页。以下是我们的图:")
                orng.InsertParagraphAfter()

                '插入一个图片并变更图片
                oshape = odoc.Bookmarks.Item("/endofdoc").Range.InlineShapes.AddOLEObject(classtype:="MSGraph.chart.8", FileName:="", linkTOFile:=False, DisplayASIcon:=False)
                ochart = oshape.OLEFormat.Object
                ochart.charttype = 4 'X1Line =4
                ochart.application.update()
                ochart.application.quit()

                '如果需要的话,从此处开始您可以在OChart之上
                '使用MiCrosoft Graph Object模型来对图进行额外的变更
                oshape.Width = oword.InchesToPoints(6.25)
                oshape.Height = oword.InchesToPoints(3.57)

                '在图之后加入文字。
                orng = odoc.Bookmarks.Item("/endofdoc").Range
                orng.InsertParagraphAfter()
                orng.InsertAfter("结束...Bye Bye...")

                '完成所有作业,关闭此窗体
                'Me.Close()

                '结束应用程序
                'Application.Exit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值