综合示例一

本节在上一节的基础上,加上了填入数据的代码,最终生成的文档如下:

请打开CaseStudy目录下的文件“基本功能演示.Table”,选择员工表,然后在命令窗口执行下面的代码:

Dim wdc As New WordCreator()
Dim
fnt1 As New Font("宋体", 16, fontstyle.Bold)
Dim
fnt2 As New Font("宋体", 9)
wdc.AddParagraph(
"员工资料卡", fnt1).Alignment = Word.RtfHorizontalAlignment.Center
wdc.Add(
New Word.Objects.RtfString("", fnt2))
Dim
tb As New Word.Objects.RtfTable(6, 5)
wdc.Add(tb)
tb.Alignment = ContentAlignment.TopCenter
'表格位置居中
Dim
widths() As Double = {55, 120.9, 55, 120.9, 115.9} '用数组存储列宽,数组之和为467.7,也就是页宽
tb.Rows(0).Cells(4).SetMerged(5, 1)
'1行第5个单元格向下合并5
tb.Rows(3).Cells(1).SetMerged(1, 3)
'4行第2个单元格向右合并3
tb.Rows(5).Cells(0).SetMerged(1, 5)
'6行第1个单元格向右合并5
For
Each tr As Word.Objects.RtfRow In tb.Rows '遍历行设置列宽
    tr.Height = 25
'设置默认行高
   
For Each cl As Word.Objects.RtfCell In tr.Cells '遍历单元格
        cl.SetRectBorder(Word.RtfBorderStyle.Single, color.Black, 1)
'设置默认边框
        cl.Alignment = ContentAlignment.MiddleLeft
'单元格内容对齐方式
       
Select Case tr.Index
           
Case 0 '正常设置第一行各单元格的宽度
                cl.Width = widths(cl.Index)
           
Case 1, 2, 4 '对于第二、第三和第五行
               
If cl.Index <> 4 Then '第五个单元格被合并了,不需要设置宽度,所以要跳过
                     cl.Width = widths(cl.Index)
'其他单元格正常设置宽度
               
End If
           
Case 3 '如果是第四行,只需设置头两个单元格
               
If cl.Index = 0 Then
                    cl.Width = widths(0)
'正常设置第一个单元格的宽度
               
ElseIf cl.Index = 1 Then '第二个单元格合并了三列,所以其宽度为合并列宽度之和
                     cl.Width = widths(1) + widths(2) + widths(3)
               
End If
           
Case 5 '第五行全部合并了,只有一个单元格,其宽度等于所有列宽之和,也就是467.7
                cl.Width = 467.7
'widths(0) + widths(1) + widths(2) + widths(3) + widths(4)
                cl.Alignment = ContentAlignment.TopLeft
       
End Select
   
Next
Next

tb.Rows(5).Height = 75
'设置第六行的高度
'
下面开始填入普通数据
Dim
dr As Row = Tables("员工").Current
tb.Rows(0).Cells(0).Content.Add(
New Word.Objects.RtfString("姓名", fnt2))
tb.Rows(0).Cells(1).Content.Add(
New Word.Objects.RtfString(dr("姓名"), fnt2))
tb.Rows(0).Cells(2).Content.Add(
New Word.Objects.RtfString("出生日期", fnt2))
tb.Rows(0).Cells(3).Content.Add(
New Word.Objects.RtfString(dr("出生日期"), fnt2))
tb.Rows(1).Cells(0).Content.Add(
New Word.Objects.RtfString("部门", fnt2))
tb.Rows(1).Cells(1).Content.Add(
New Word.Objects.RtfString(dr("部门"), fnt2))
tb.Rows(1).Cells(2).Content.Add(
New Word.Objects.RtfString("雇佣日期", fnt2))
tb.Rows(1).Cells(3).Content.Add(
New Word.Objects.RtfString(dr("雇佣日期"), fnt2))
tb.Rows(2).Cells(0).Content.Add(
New Word.Objects.RtfString("性别", fnt2))
tb.Rows(2).Cells(1).Content.Add(
New Word.Objects.RtfString(dr("性别"), fnt2))
tb.Rows(2).Cells(2).Content.Add(
New Word.Objects.RtfString("职务", fnt2))
tb.Rows(2).Cells(3).Content.Add(
New Word.Objects.RtfString(dr("职务"), fnt2))
tb.Rows(3).Cells(0).Content.Add(
New Word.Objects.RtfString("地址", fnt2))
tb.Rows(3).Cells(1).Content.Add(
New Word.Objects.RtfString(dr("地址"), fnt2))
tb.Rows(4).Cells(0).Content.Add(
New Word.Objects.RtfString("家庭电话", fnt2))
tb.Rows(4).Cells(1).Content.Add(
New Word.Objects.RtfString(dr("家庭电话"), fnt2))
tb.Rows(4).Cells(2).Content.Add(
New Word.Objects.RtfString("办公电话", fnt2))
tb.Rows(4).Cells(3).Content.Add(
New Word.Objects.RtfString(dr("办公电话"), fnt2))
tb.Rows(5).Cells(0).Content.Add(
New Word.Objects.RtfString(dr("备注"), fnt2))
'
下面将照片显示在表格中
Dim
img As Image = GetImage(dr("照片"))
Dim
pic As New Word.Objects.RtfPicture(img)
pic.Height = 120
tb.Rows(0).Cells(4).Content.Add(pic)
tb.Rows(0).Cells(4).Alignment = ContentAlignment.MiddleCenter

'
保存并打开文件
Dim
fl As String = "c:\temp\test.docx"
wdc.Save(fl)
Process.Start(fl)

 

 


本页地址:http://www.foxtable.com/webhelp/topics/6043.htm