老师好,我的专业报表,出现0值情况,在您指导下,问题解决,所用代码为For r As Integer = 0 To tb.Rows.Count - 1
If tb.Rows(r).isnull(ColNames(c)) = False
rt.Cells(r + 1, c).Text = tb.Rows(r)(ColNames(c))
End If
我现在把合并单元格代码融合进来后,又出现了0值,上述代码还在,不知为什么
代码如下:
Syscmd.Column.UnHide()
Dim doc As New PrintDoc '定义一个新报表
Dim rt As New prt.RenderTable '定义一个新表格
Dim tb As Table = CurrentTable
Dim ColNames As New List(Of String)
For Each cl As Col In tb.Cols '排除隐藏列
If cl.Visible Then
ColNames.Add(cl.Name)
End If
Next
Dim rx As New prt.RenderText
rx.Text = ApplicationTitle.split(".")(0)
rx.Style.Font = New Font("宋体", 16, FontStyle.Bold)
rx.Style.TextAlignHorz = prt.AlignHorzEnum.Center
rx.Style.Spacing.Bottom = 3
Doc.body.Children.Add(rx)
rt.SplitHorzBehavior = prt.SplitBehaviorEnum.SplitIfNeeded '表格宽度超出页宽时,可以水平换页
rt.Style.Font = tb.Font
Dim MergeCols As Integer = 1 '指定要合并的列数
tb.Sort = "计算部位" '根据合并列排序
rt.SplitHorzBehavior = prt.SplitBehaviorEnum.SplitIfNeeded
rt.Style.Font = tb.Font
rt.Style.TextAlignVert = prt.AlignVertEnum.Center
rt.RepeatGridLinesVert = True '换页后重复表格线
For c As Integer = 0 To ColNames.Count - 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 tb.Rows(r).isnull(ColNames(c)) = False
rt.Cells(r + 1, c).Text = tb.Rows(r)(ColNames(c))
End If
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
For c As Integer = 0 To ColNames.Count - 1 '逐列设置和填入内容
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.Center '数据水平靠右
End If
For r As Integer = 0 To tb.Rows.Count - 1 '开始填入该列内容
If tb.Rows(r).isnull(ColNames(c)) = False
rt.Cells(r + 1, c).Text = tb.Rows(r)(ColNames(c))
End If
If ColNames(c) = "重量t" OrElse ColNames(c) = "油漆面积㎡"
rt.Cells(r + 1, c).Text = format(tb.Rows(r)(ColNames(c)),"0.000")
End If
Next
Next
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 '逐行写入行号
rt.Cols(0).Style.TextAlignHorz = prt.AlignHorzEnum.Center
Next
rt.Style.GridLines.Horz = new Prt.LineDef(Color.Gray) '这个设置的是所有的水平网格线
rt.Style.GridLines.Vert = new Prt.LineDef(Color.Gray) '这个设置的是所有的垂直网格线
rt.Style.GridLines.Left= New Prt.LineDef(0.5, Color.Black)
rt.Style.GridLines.Right = New Prt.LineDef(0.5, Color.Black)
rt.y = "Prev.Bottom + 3" '设置垂直位置
rt.CellStyle.Spacing.All = 1 '单元格内距设为0.5毫米
rt.Rows(0).Style.TextAlignHorz = prt.AlignHorzEnum.Center '第一行内容水平居中
rt.Rows(0).Style.GridLines.top = New Prt.LineDef(0.5, Color.Black)
rt.RowGroups(0,1).Header = prt.TableHeaderEnum.All '利用行组,将第一行设为表头.
doc.Body.Children.Add(rt) '将表格加入到报表
doc.PageSetting.Landscape = False '横向打印
Doc.PageSetting.TopMargin = 20 '设置上边距
Doc.PageSetting.BottomMargin = 20 '设置下边距
rt.Rows(rt.Rows.Count).Height = 0.01
Dim cnt As Integer = rt.Rows.Count-1
'rt.Rows(rt.Rows.Count-1).Style.BackColor = Color.white
'rt.Rows(cnt).Style.GridLines.Top = New Prt.LineDef(Color.white)
For i As Integer = 0 To rt.Cols.Count - 1
If i = 0
rt.Cells(cnt,i).Style.GridLines.left= New Prt.LineDef(0.5,Color.Black)
Else
rt.Cells(cnt,i).Style.GridLines.left= New Prt.LineDef(Color.Gray)
End If
If i = rt.Cols.Count - 1
rt.Cells(cnt,i).Style.GridLines.right= New Prt.LineDef(0.5,Color.Black)
End If
rt.Cells(cnt,i).Style.GridLines.Top= New Prt.LineDef(Color.white)
rt.Cells(cnt,i).Style.GridLines.Bottom = New Prt.LineDef(0.5,Color.Black)
Next
rt.RowGroups(rt.Rows.Count-1,1).Footer = prt.TableHeaderEnum.All
rt.Rows(cnt).Style.GridLines.Bottom = New Prt.LineDef(Color.Black)
Dim my As New prt.RenderTable
my.Cells(0,0).Text = Date.Today
my.Cells(0,1).Text = ApplicationTitle.split(".")(0)
my.Cells(0,2).Text = "第[PageNo]页,共[PageCount]页"
my.Cols(0).Style.TextAlignHorz = prt.AlignHorzEnum.Left
my.Cols(1).Style.TextAlignHorz = prt.AlignHorzEnum.Center
my.Cols(2).Style.TextAlignHorz = prt.AlignHorzEnum.Right
my.Style.FontSize = 8 '字体大小为8磅
Doc.PageFooter = my '作为页眉使用
doc.Preview()