vb.net word操作:填充职员表并打印

选自:《Visual Basic.Net 循序渐进》
 

【例 21.12【项目:code21-012】填充职员表并打印。

本例中类似【例 21.7】,略有简化,实现了数据填充表格,表格内插入图片,以及实现文档打印。窗体设计如下图所示(注意:为了演示方便,已经填充了数据):

图21-17 窗体设计

在本例中还需要掌握的知识:

1、插入图片到指定位置。

Range.InlineShapes.AddPicture (FileName, LinkToFile, SaveWithDocument, Range)

  1. 参数FileName:必选,图片全路径。
  1. 参数LinkToFile:必选,如果为True,则建立图片与其源文件之间的链接关系。否则,使图片成为其源文件的独立副本。通常设置为True。
  2. 参数SaveWithDocument:必选,如果是True,则将图片与文档一起保存。否则,只链接到图片文件。通常设置为True。
  3. 参数Range:可选,图片置于文本中的位置。如果该区域未折叠,那么图片将覆盖此区域,否则插入图片。如果省略此参数,则自动放置图片。
  4. 返回值:返回一个代表该图片的 InlineShape对象。

2、打印输出。

打印输出主要使用Document.PrintOut 方法,它提供了多达19个可选参数。不同于【例 21.7】中将参数全部加入,本例中只加入需要的参数,具体语法为:

Word.Document.PrintOut(参数名称1:=值1, 参数名称2:=值2,……)

请参看本例详细代码。

要使用到的两个参数:

  1. 参数OutputFileName:可选,如果PrintToFile 设为True,则该参数指定要打印到的文件名。
  2. 参数PrintToFile:可选,如果为True,则将打印指令发送到文件。请确保使用OutputFileName指定文件名。如果没有指定 PrToFileName,将提示用户输入要使用的输出文件的文件名。

详细代码如下(说明:教材中配套代码附上了详细说明,这里简化了):

    Dim modelFile As String = "d:\lessons\职员信息登记表.docx"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim modelfilePath As String = (New IO.FileInfo(modelFile)).Directory.FullName
        Dim wordApp As New Word.Application
        Dim doc As Word.Document
        doc = wordApp.Documents.Open(modelFile)

        Dim tb As Word.Table
        tb = doc.Tables(1)

        tb.Cell(1, 2).Range.Text = txtXm.Text
        tb.Cell(1, 4).Range.Text = txtXb.Text
        tb.Cell(1, 6).Range.Text = txtMz.Text
        tb.Cell(2, 2).Range.Text = txtJkzk.Text
        tb.Cell(2, 4).Range.Text = txtCsny.Text
        tb.Cell(2, 6).Range.Text = txtHyzk.Text
        tb.Cell(3, 2).Range.Text = txtWhcd.Text
        tb.Cell(3, 4).Range.Text = txtByyx.Text
        tb.Cell(4, 2).Range.Text = txtGzbm.Text
        tb.Cell(4, 4).Range.Text = txtZw.Text
        tb.Cell(4, 6).Range.Text = txtRzsj.Text
        tb.Cell(5, 2).Range.Text = txtLxdh.Text
        tb.Cell(5, 4).Range.Text = txtLxdz.Text

        Dim dCell As Word.Cell
        dCell = tb.Cell(1, 7)

        Dim employeePic As Word.InlineShape
        employeePic = dCell.Range.InlineShapes.AddPicture("d:\lessons\Employee\" & txtXm.Text & ".jpg", False)
        employeePic.Width = dCell.Width - 8
        employeePic.Height = tb.Cell(1, 1).Height + tb.Cell(2, 1).Height + tb.Cell(3, 1).Height + tb.Cell(4, 1).Height - 8     'dCell.Height

        doc.SaveAs2(modelfilePath & "\Employee\" & txtXm.Text & ".doc", Word.WdSaveFormat.wdFormatDocument)
        wordApp.Quit()
        MessageBox.Show("输出完成")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim printFile As String = "d:\lessons\Employee\" & txtXm.Text & "1.oxps"
        Dim wordApp As New Word.Application
        Dim doc As Word.Document = wordApp.Documents.Open("d:\lessons\Employee\" & txtXm.Text)

        Dim pagecount As Integer = 1    'doc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages)
        doc.PrintOut(PrintToFile:=True, OutputFileName:=printFile)
        wordApp.Quit()
        MessageBox.Show("打印完成")
    End Sub

打印的效果如下:

图21-18 打印生成的文件

其实以上代码很方便与数据库结合,批量输出文档或者打印。

Visual Basic.Net 循序渐进》的下载地址:
链接:https://pan.baidu.com/s/1IfaLvlklx-nT4KK4VKZuIw 
提取码:ip5n

目前教程提供了学习章节共计569节,提供了示例502个,并在不断扩充中。
这是博主自己总结了多年VB开发经验后,结合实际开发编著的教程。
《Visual Basic.Net 循序渐进》前言
《Visual Basic.Net 循序渐进》详细目录

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值