Foxtable(狐表)用户栏目专家坐堂 → 专业报表合并单元格的完美通用代码


  共有16272人关注过本帖平板打印复制链接

主题:专业报表合并单元格的完美通用代码

帅哥哟,离线,有人找我吗?
狐狸爸爸
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251054 威望:0 精华:91 注册:2008/6/17 17:14:00
专业报表合并单元格的完美通用代码  发帖心情 Post By:2010/5/26 15:04:00 [只看该作者]

下面的代码打印订单表,并合并前两列,也就是产品和客户两列:

 

Dim doc As New PrintDoc
Dim
rt As New prt.RenderTable
Dim
tb As Table = Tables("订单")
Dim
ColNames As String() = New String(){"产品", "客户","单价","折扣","数量","金额","日期"}
Dim MergeCols As Integer = 2 '指定要合并的列数
tb.Sort = "产品,客户" '根据合并列排序
rt.Width =
"Auto"
rt.CanSplitHorz =
True
rt.Style.Font = tb.Font
rt.Style.TextAlignVert = prt.AlignVertEnum.Center
rt.RepeatGridLinesVert =
True '换页的时候重复表格线
For
c As Integer = 0 To ColNames.Length - 1
   
Dim lr As Integer ' 用于保存合并区域的起始行
    rt.Cells(
0,c).Text = ColNames(c)
    rt.Cells(
0,c).Style.TextAlignHorz = prt.AlignHorzEnum.Center
    rt.Cols(c).Width = tb.Cols(ColNames(c)).PrintWidth

    If
tb.Cols(ColNames(c)).IsNumeric Orelse tb.Cols(ColNames(c)).IsDate Then
        rt.Cols(c).Style.TextAlignHorz = prt.AlignHorzEnum.Right

    End
If
    For
r As integer = 0 To tb.Rows.Count - 1
       
If c <= MergeCols - 1 then '如果是要合并的列
           
Dim Merge As Boolean = True
            If
r = 0 Then
                Merge =
False
            Else
                For
n As Integer = 0 To c
                    if
tb.Rows(r)(ColNames(n)) <> tb.Rows(r - 1)(ColNames(n))
                        Merge =
False
                        Exit
For
                    End
If
                Next
            End
If
            If
Merge Then
                rt.Cells(lr,c).SpanRows = rt.Cells(lr,c).SpanRows +
1
           
Else
                rt.Cells(r +
1, c).Text = tb.Rows(r)(ColNames(c))
                rt.Cells(r +
1, c).VertSplitBehavior = prt.CellSplitBehaviorEnum.Copy
                lr = r +
1
           
End If
        Else

            rt.Cells(r +
1, c).Text = tb.Rows(r)(ColNames(c))
        End
If
    Next
Next

rt.Style.Gridlines.All = New prt.Linedef(Color.Gray)
rt.CellStyle.Spacing.All =
0.5
rt.Rows(
0).Style.TextAlignHorz = prt.AlignHorzEnum.Center
rt.RowGroups(
0,1).Header = prt.TableHeaderEnum.All
doc.Body.Children.Add(rt)
doc.Preview()

 

上面的代码是通用的,要打印其它表格或者合并不同的列时,只需修改下面四行代码:

 

Dim tb As Table = Tables("订单")
Dim
ColNames As String() = New String(){"产品", "客户","单价","折扣","数量","金额","日期"}
Dim MergeCols As Integer = 2 '指定要合并的列数
tb.Sort = "产品,客户" '根据合并列排序

 

注意指定要打印列的时候,要合并的列必须位于最前面。


[本帖被加为精华]
 回到顶部
总数 12 1 2 下一页