随心所欲的表

既然是自己绘制表格,就可以随心所欲地绘制各种异形表格

示例一

这个示例用一个数组表示一行,数组第第一个元素表示行高,从第二个元素开始,表示各单元格的宽度比例。

例如下面的数组:

{20, 0.15, 0.15, 0.25, 0.3}

表示行高为20,四个单元格宽度占表宽的比例分解为15%、15%、25%、30%,剩余15%留空。

用负数表示留空的比例,例如:

{50, -0.25, 0.5}

表示行高为50,先留空25%的表宽,第一个单元格的宽度为表宽的50%。

下面是测试代码,请在命令窗口执行:

Dim tableCells As New List(Of Double()) '定义一个集合,集合的元素是Double数组,一个数组表示一行
tableCells.Add(
New Double(){20, 0.2, 0.2, 0.2, 0.3, 0.1})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3})
tableCells.Add(
New Double(){25, 0.2, 0.2, 0.2})
tableCells.Add(
New Double(){50, - 0.25, 0.5})
tableCells.Add(
New Double(){25, 0.2, 0.2, 0.2})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3, 0.15})
Dim
file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
rectPage As RectangleF = pdc.PageRectangle
rectPage.Inflate( - 72, - 72)

Dim
rectCell As RectangleF = rectPage
For
Each cells() As Double In tableCells
    rectCell.Height = cells(0)
'数组第一个元素为行高
   
For c As Integer = 1 To cells.Length - 1 '绘制单元格,注意从1而不是0开始遍历
        rectCell.Width = math.Abs(cells(c) * rectPage.Width)
'从第二个元素开始为列宽
       
If cells(c) > 0 Then '如果宽度占比为正
            pdc.DrawRectangle(pens.Black, rectCell)
'绘制单元格
       
End If
        rectCell.Offset(rectcell.Width, 0)
'移到下一单元格
   
Next
    rectCell.Offset(0, rectCell.Height)
'移到下一行
    rectCell.X = rectPage.X
'单元格回到水平初始位置,准备绘制下一行
Next

pdc.Save(file)
'保存文件
Process.Start(file)
'打开文件

生成的表格为:

示例二

和示例一基本相似,只是表方向改为纵向,

例如下面的数组:

{20, 0.15, 0.15, 0.25, 0.3}

表示列宽为20,四个单元格高度占表高的比例分解为15%、15%、25%、30%,剩余15%留空,所以这个例子要指定表高。

下面是完整的代码:

Dim tableCells As New List(Of Double()) '定义一个集合,集合的元素是Double数组,一个数组表示一列
tableCells.Add(
New Double(){20, 0.2, 0.2, 0.2, 0.3, 0.1})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3})
tableCells.Add(
New Double(){25, 0.2, 0.2, 0.2})
tableCells.Add(
New Double(){50, - 0.25, 0.5})
tableCells.Add(
New Double(){25, 0.2, 0.2, 0.2})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3})
tableCells.Add(
New Double(){20, 0.15, 0.15, 0.25, 0.3, 0.15})
Dim
file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
rectPage As RectangleF = pdc.PageRectangle
rectPage.Inflate( - 72, - 72)

Dim
rectCell As RectangleF = rectPage
Dim
tableHeight As Integer = 468 '指定表高
For
Each cells() As Double In tableCells
    rectCell.Width = cells(0)
'数组第一个元素为列宽
   
For r As Integer = 1 To cells.Length - 1 '绘制单元格 ,注意从1而不是0开始遍历
        rectCell.Height = math.Abs(cells(r) * tableHeight)
'从第二个元素开始为单元格高度
       
If cells(r) > 0 Then '如果高度占比为正
            pdc.DrawRectangle(pens.Black, rectCell)
'绘制单元格
       
End If
        rectCell.Offset(0, rectCell.Height)
'移到下一单元格
   
Next
    rectCell.Offset(rectcell.Width, 0)
'移到下一列
    rectCell.Y = rectPage.Y
'单元格回到垂直初始位置,准备绘制下一列
Next

pdc.Save(file)
'保存文件
Process.Start(file)
'打开文件

生成的文档为:


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