以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  给大家分享一个懒人键!  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=132531)

--  作者:fangdejin
--  发布时间:2019/3/24 9:59:00
--  给大家分享一个懒人键!
有时打印不想每次都设置标题,而且文本也不居中,也没有人数,和数值列的统计,现在有了,生成预想的格式。

代码如下:
Dim doc As New PrintDoc \'定义一个新报表
Dim rt As New prt.RenderTable \'定义一个新表格
rt.Style.GridLines.All = New prt.Linedef \'设置网格线
rt.CellStyle.Spacing.All = 0.5 \'内容距离网格线0.5毫米
rt.Style.TextAlignVert = prt.AlignVertEnum.Center \'内容垂直居中
rt.Rows(0).Style.BackColor = Color.LightGray \'第一行背景颜色设为灰色.
rt.RowGroups(0,1).Header = prt.TableHeaderEnum.All \'将第一行作为表头.
rt.ColGroups(0,1).Header = prt.TableHeaderEnum.All \'将第一列作为表头.
-------------------------------------------------------------------------------------------------------这是代码生成报表的基本格式,自己可以根据帮助文件改写
Dim cnt As Integer
Dim drs As List(Of DataRow)
Dim tbl As Table = Tables("客户资料_Table1")
-------------------------------------------------------------------------------------------------------定义一些参数
Dim rx As New prt.RenderText \'定义一个文本对象
rx.text = "客户股票汇总"
rx.Style.FontBold = True \'字体加粗
rx.Style.FontSize = 18 \'大体大小为16磅
rx.Style.TextAlignHorz = prt.AlignHorzEnum.Center \'水平居中排列
rx.Style.Spacing.Bottom = 3 \'和下面的对象(表格)距离3毫米
doc.Body.Children.Add(rx) \'加入
------------------------------------------------------------------------------------------------------定义标题参数
Dim ColNames As String() = New String(){"序号","姓名","时间","子公司","套餐","股票","备注"}
rt.Width = "Auto" \'表格宽度为自动,也就是等于各列设置宽度之和
rt.SplitHorzBehavior = prt.SplitBehaviorEnum.SplitIfNeeded \'表格宽度超出页宽时,可以水平换页
rt.Style.Font = tbl.Font
rt.Cols.Insert(0) \'在左边插入一列,用于打印行号
rt.Cols(0).Width = 10 \'设置行号列的宽度
------------------------------------------------------------------------------------------------------定义要打印的列
For i As Integer = 1 To rt.Rows.Count - 1
    rt.Cells(i,0).text = i \'逐行写入行号
Next
For c As Integer = 0 To 6
 rt.Cols(c).Width = 13 \'明确指定每一列的宽度
 If c = 0 Then
 rt.Cells(0,0). Text = "序号"
 Else 
 rt.Cells(0, 1).Text = "姓名"
 rt.Cells(0, 2).Text = "时间"
 rt.Cells(0, 3).Text = "子公司"
 rt.Cells(0, 4).Text = "套餐"
 rt.Cells(0, 5).Text = "股票"
 rt.Cells(0, 6).Text = "备注"
  End If
Next
------------------------------------------------------------------------------------------------------定义列的标题

For c As Integer = 0 To ColNames.Length - 1 \'逐列设置和填入内容
rt.Cells(0,c).Text = ColNames(c) \'列名作为标题
rt.Cells(0,c).Style.TextAlignHorz = prt.AlignHorzEnum.Center \'标题内容水平居中
rt.Cols(c).Width = tbl.Cols(ColNames(c)).PrintWidth \'列宽等于实际列宽
If tbl.Cols(ColNames(c)).IsNumeric OrElse tbl.Cols(ColNames(c)).IsDate Then \'如果是数值或日期列
rt.Cols(c).Style.TextAlignHorz = prt.AlignHorzEnum.Center \'数据水平居中
End If
For r As Integer = 0 To tbl.Rows.Count -1 \'开始填入该列内容
rt.Cells(r + 1, c).Text = tbl.Rows(r)(ColNames(c))
 Next
Next
------------------------------------------------------------------------------------------------------定义各列打印的格式
cnt = rt.Rows.Count
rt.Cells(cnt, 0).Text = "合计 " \'打印合计
rt.Cells(cnt, 1).Text = tbl.compute("Count(姓名)")
rt.Cells(cnt, 5).Text = tbl.compute("Sum(股票)")
rt.Style.Gridlines.All = New prt.Linedef(Color.Gray) \'灰色网格线
------------------------------------------------------------------------------------------------------最后一行你要做的各种统计,自己根据需要编写。
rt.Style.TextAlignHorz = prt.AlignHorzEnum.Center \'全表之行内容水平居中
doc.Body.Children.Add(rt) \'将表格加入到报表
rt.Rows(Cnt).Style.BackColor = Color.Orange \'统计行背景颜色设为橙色.
\'如果需要分栏打印,请加上下面两行
\'doc.Columns.Add()
\'doc.Columns.Add()
doc.Preview()
[此贴子已经被作者于2019/3/24 12:20:33编辑过]

--  作者:有点甜
--  发布时间:2019/3/24 11:27:00
--  
图片点击可在新窗口打开查看 谢谢分享
--  作者:fangdejin
--  发布时间:2019/3/24 12:16:00
--  
谢谢版主,毕竟跟着大家也学到很多~