打印
Namespace Web Public Class CreateTableTools Inherits System.Web.UI.Page '**************************************** '****** 功能名称:打印创建Table功能类 '****** 功能描述:创建打印需要的创建的Table '****** '****** 开发日期:2013年9月25日 13:34:35 '****** 开 发 人:吴翰哲 '****** '****** 开发版权:Pantosoft Co.,Ltd '**************************************** Dim i As Integer = 0 ' 填充数据的起始索引 Dim j As Integer = 0 ' 填充数据的末尾索引 Dim tr As TableRow Dim td As TableCell Dim table As Table '//创建表 '//参数说明 '/**Form: 所需要填充数据的容器 '**ds: 所需要打印的数据集 '**dataViewName: 数据集ds的中的具体数据表明 '**title: 所需要打印的标题列的显示名称 '**setPageColumn: 设置每页打印需要显示的行数 '**parm1~parm7: 每行的单元格所显示的数据表中的字,有几个参数就填几个,剩下的填入Nothing即可,上限是7个参数 '**ZDName1~ZDName7:表格各个单元格对应parm1~parm7的表头显示名称 '**/ Public Sub CreateTable(ByVal Form As System.Web.UI.HtmlControls.HtmlForm, ByVal ds As DataSet, ByVal dataViewName As String, _ ByVal title As String, ByVal setPageColumn As Integer, ByVal parm1 As String, ByVal parm2 As String, _ ByVal parm3 As String, ByVal parm4 As String, ByVal parm5 As String, ByVal parm6 As String, ByVal parm7 As String, _ ByVal ZDName1 As String, ByVal ZDName2 As String, ByVal ZDName3 As String, ByVal ZDName4 As String, ByVal ZDName5 As String, ByVal ZDName6 As String, ByVal ZDName7 As String) Dim dsCount As Integer = ds.Tables(dataViewName).Rows.Count If dsCount > 0 Then '//Start IF 判断ds中是否有数据 If dsCount <= setPageColumn Then '//Start If 判断显示行是否小于等于ds中的行数 table = New Table table.BorderWidth = New Unit(1) table.CellSpacing = 0 '设置单元格间距 table.CellPadding = 5 '设置单元格内容与边框之间的边距 table.BorderStyle = BorderStyle.Solid table.GridLines = GridLines.Both table.BorderColor = Color.Black '//调用 CreateTableZDName方法生成表头 CreateTableZDName(table, title, ZDName1, ZDName2, ZDName3, ZDName4, ZDName5, ZDName6, ZDName7) '//开始创建数据行和填充数据 For Each dr As DataRow In ds.Tables(dataViewName).Rows '//Start For 循环创建行 tr = New TableRow If Not String.IsNullOrEmpty(parm1) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm1).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm2) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm2).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm3) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm3).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm4) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(200) td.Text = dr.Item(parm4).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm5) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm5).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm6) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm6).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm7) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = dr.Item(parm7).ToString() tr.Cells.Add(td) End If table.Rows.Add(tr) Form.Controls.Add(table) '//向Form中填充table Next '//End For 循环创建行 Else '//如果每页显示行大于ds中的行数,则根据设置的显示行(setPageColumn)进行分页 For Index As Integer = 0 To dsCount - 1 '//Start For 循环遍历数据 If Index Mod setPageColumn = 0 AndAlso setPageColumn <> 0 AndAlso Index <> 0 Then '//Start If 如果能被整除则创建分页所需的Table并填充数据 table = New Table table.BorderWidth = New Unit(1) table.CellSpacing = 0 '设置单元格间距 table.CellPadding = 5 '设置单元格内容与边框之间的边距 table.BorderStyle = BorderStyle.Solid table.GridLines = GridLines.Both table.BorderColor = Color.Black ' //调用 CreateTableZDName方法生成表头 CreateTableZDName(table, title, ZDName1, ZDName2, ZDName3, ZDName4, ZDName5, ZDName6, ZDName7) j = Index '//记录每次循环被整除时的index值 For Index1 As Integer = i To j '//Start For 循环填充数据 tr = New TableRow If Not String.IsNullOrEmpty(parm1) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm1).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm2) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm2).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm3) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm3).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm4) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(200) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm4).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm5) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm5).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm6) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm6).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm7) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(Index1).Item(parm7).ToString() tr.Cells.Add(td) End If If Index1 = j Then i = j + 1 '//每循环一次就把此次循环结束的索引赋给 i 以进行下一轮循环 来给对应单元格填充对应数据 End If table.Rows.Add(tr) Next '//End For 循环填充数据 Form.Controls.Add(table) Dim lab As New Label '创建一个Label添加到From中以实现自动分页 lab.Text = "<div style=" & Chr(34) & "page-break-before:always" & Chr(34) & "></div><br/><br/>" Form.Controls.Add(lab) End If '//Start If 如果能被整除则创建分页所需的Table并填充数据 Next '//End For 循环遍历数据 If dsCount Mod setPageColumn <> 0 Then '//判断能否被ds中数据行能否被setPageColumn整除,如果不能,则补全不能被整除剩下的数据 table = New Table table.BorderWidth = New Unit(1) table.CellSpacing = 0 '设置单元格间距 table.CellPadding = 5 '设置单元格内容与边框之间的边距 table.BorderStyle = BorderStyle.Solid table.GridLines = GridLines.Both table.BorderColor = Color.Black CreateTableZDName(table, title, ZDName1, ZDName2, ZDName3, ZDName4, ZDName5, ZDName6, ZDName7) '开始填充剩下的数据 For index2 As Integer = j + 1 To dsCount - 1 '//Start For 循环添加数据 tr = New TableRow If Not String.IsNullOrEmpty(parm1) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm1).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm2) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm2).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm3) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm3).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm4) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(200) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm4).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm5) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm5).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm6) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm6).ToString() tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(parm7) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ds.Tables(dataViewName).Rows(index2).Item(parm7).ToString() tr.Cells.Add(td) End If table.Rows.Add(tr) Next '//End For 循环添加数据 Form.Controls.Add(table) Dim lab As New Label '创建一个Label添加到From中以实现自动分页 lab.Text = "<div style=" & Chr(34) & "page-break-before:always" & Chr(34) & "></div><br/><br/>" Form.Controls.Add(lab) End If '//End If 判断能否被ds中数据行能否被setPageColumn整除,如果不能,则补全不能被整除剩下的数据 End If '//End If 判断显示行是否小于等于ds中的行数 Else '如果ds中没有数据则填出提示框提示用户 Page.ClientScript.RegisterStartupScript(Me.GetType(), "Alert", "<script>alert('需要打印的数据为空!')</script>") End If '//End IF 判断ds中是否有数据 End Sub '//创建表头 Public Sub CreateTableZDName(ByVal table As Table, ByVal title As String, ByVal ZDName1 As String, ByVal ZDName2 As String, ByVal ZDName3 As String, ByVal ZDName4 As String, ByVal ZDName5 As String, ByVal ZDName6 As String, ByVal ZDName7 As String) table.BorderWidth = New Unit(1) tr = New TableRow td = New TableCell '如果传入标题参数不为nothing则创建单元格 If Not String.IsNullOrEmpty(ZDName1) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName1 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName2) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName3 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName3) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName3 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName4) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(200) td.Text = ZDName4 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName5) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName5 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName6) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName6 tr.Cells.Add(td) End If If Not String.IsNullOrEmpty(ZDName7) Then td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("12px") td.Font.Name = "宋体" td.Width = New Unit(100) td.Text = ZDName7 tr.Cells.Add(td) End If table.Rows.Add(tr) '将生成的row添加到table中 '//判断是否传入标题,如果有则生成标题列 If Not String.IsNullOrEmpty(title) Then tr = New TableRow td = New TableCell tr.HorizontalAlign = HorizontalAlign.Center td.HorizontalAlign = HorizontalAlign.Center td.Font.Size = New FontUnit("14px") td.Font.Name = "宋体" td.ColumnSpan = table.Rows(0).Cells.Count td.Text = title tr.Cells.Add(td) table.Rows.AddAt(0, tr) End If End Sub ''' <summary> ''' 建峰需求 每行增加空单元格 ''' </summary> ''' <remarks></remarks> Public Sub CreateByTable() End Sub End Class End Namespace