首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

哪位朋友能提供一个datagrid打印的类或脚本,要vb.net的!该如何处理

2012-01-02 
哪位朋友能提供一个datagrid打印的类或脚本,要vb.net的!哪位朋友能提供一个datagrid打印的类或脚本,要vb.n

哪位朋友能提供一个datagrid打印的类或脚本,要vb.net的!
哪位朋友能提供一个datagrid打印的类或脚本,要vb.net的,谢谢!

[解决办法]
可以把DataGrid输出至Word或是Excel之后,打印.
参考:
http://blog.csdn.net/insus/archive/2008/03/31/2231945.aspx
[解决办法]

VB.NET code
 '以下为模块内部使用     Private ev As PrintPageEventArgs     Private PrintDocument1 As PrintDocument     Private DataGridColumn As DataColumn     Private DataGridRow As DataRow     Private DataTable1 As DataTable     Private Cols As Integer                             '当前要打印的列     Private Rows As Integer = 0                         '当前要打印的行     Private ColsCount As Integer                        '当前DATAGRID共有多少列     Private PrintingLineNumber As Integer = 0           '当前正要打印的行号     Private PageRecordNumber As Integer                 '当前要所要打印的记录行数,由计算得到.     Private PrintingPageNumber As Integer = 0           '正要打印的页号     Private PageNumber As Integer                       '共需要打印的页数     Private PrintRecordLeave As Integer                 '当前还有多少页没有打印     Private PrintRecordComplete As Integer = 0         '已经打印完的记录数     Private Pleft As Integer     Private Ptop As Integer     Private Pright As Integer     Private Pbottom As Integer     Private Pwidth As Integer     Private Pheigh As Integer     Private DrawBrush As New SolidBrush(System.Drawing.Color.Black)     '当前画笔颜色     Private PrintRecordNumber As Integer '每页打印的记录条数     Private Totalpage As Integer '总共应该打印的页数     Sub New(ByVal TableSource As DataTable)         DataTable1 = New DataTable         DataTable1 = TableSource         ColsCount = DataTable1.Columns.Count     End Sub     '用户自定义字体及字号     Public WriteOnly Property SetTableFont() As System.Drawing.Font         Set(ByVal Value As System.Drawing.Font)             TableFont = Value         End Set     End Property     Public WriteOnly Property SetHeadFont() As System.Drawing.Font         Set(ByVal Value As System.Drawing.Font)             HeadFont = Value         End Set     End Property     Public WriteOnly Property SetSubHeadFont() As System.Drawing.Font         Set(ByVal Value As System.Drawing.Font)             SubHeadFont = Value         End Set     End Property     Public WriteOnly Property SetFootFont() As System.Drawing.Font         Set(ByVal Value As System.Drawing.Font)             FootFont = Value         End Set     End Property     Public WriteOnly Property SetSubFootFont() As System.Drawing.Font         Set(ByVal Value As System.Drawing.Font)             SubFootFont = Value         End Set     End Property     Public WriteOnly Property SetHeadText() As String         Set(ByVal Value As String)             HeadText = Value         End Set     End Property     Public WriteOnly Property SetSubHeadLeftText() As String         Set(ByVal Value As String)             SubHeadLeftText = Value         End Set     End Property     Public WriteOnly Property SetSubHeadRightText() As String         Set(ByVal Value As String)             SubHeadRightText = Value         End Set     End Property     Public WriteOnly Property SetFootText() As String         Set(ByVal Value As String)             FootText = Value         End Set     End Property     Public WriteOnly Property SetSubFootLeftText() As String         Set(ByVal Value As String)             SubFootLeftText = Value         End Set     End Property     Public WriteOnly Property SetSubFootRightText() As String         Set(ByVal Value As String)             SubfootRightText = Value         End Set     End Property     Public WriteOnly Property SetHeadHeight() As Integer         Set(ByVal Value As Integer)             HeadHeight = Value         End Set     End Property     Public WriteOnly Property SetSubHeadHeight() As Integer         Set(ByVal Value As Integer)             SubHeadHeight = Value         End Set     End Property     Public WriteOnly Property SetFootHeight() As Integer         Set(ByVal Value As Integer)             FootHeight = Value         End Set     End Property     Public WriteOnly Property SetSubFootHeight() As Integer         Set(ByVal Value As Integer)             SubFootHeight = Value         End Set     End Property     Public WriteOnly Property SetCellHeight() As Integer         Set(ByVal Value As Integer)             Y_unit = Value         End Set     End Property     Public Sub Print()         Try             PrintDocument1 = New Printing.PrintDocument             AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage             Dim PageSetup As PageSetupDialog             PageSetup = New PageSetupDialog             PageSetup.Document = PrintDocument1             PrintDocument1.DefaultPageSettings = PageSetup.PageSettings             If PageSetup.ShowDialog() = DialogResult.Cancel Then                 Exit Sub             End If             Pleft = PrintDocument1.DefaultPageSettings.Margins.Left             Ptop = PrintDocument1.DefaultPageSettings.Margins.Top             Pright = PrintDocument1.DefaultPageSettings.Margins.Right             Pbottom = PrintDocument1.DefaultPageSettings.Margins.Bottom             Pwidth = PrintDocument1.DefaultPageSettings.Bounds.Width             Pheigh = PrintDocument1.DefaultPageSettings.Bounds.Height             '将当前页分成基本的单元             X_unit = (Pwidth - Pleft - Pright) / DataTable1.Columns.Count - 1             PrintRecordNumber = (Pheigh - Ptop - Pbottom - HeadHeight - SubHeadHeight - FootHeight - SubFootHeight - Y_unit) \ Y_unit             If DataTable1.Rows.Count > PrintRecordNumber Then                 If DataTable1.Rows.Count Mod PrintRecordNumber = 0 Then                     Totalpage = DataTable1.Rows.Count \ PrintRecordNumber                 Else                     Totalpage = DataTable1.Rows.Count \ PrintRecordNumber + 1                 End If             Else                 Totalpage = 1             End If             PrintDocument1.DocumentName = Totalpage.ToString             Dim PrintPriview As PrintPreviewDialog             PrintPriview = New PrintPreviewDialog             PrintPriview.Document = PrintDocument1             PrintPriview.WindowState = FormWindowState.Maximized             PrintPriview.ShowDialog()         Catch ex As Exception             MsgBox("打印错误,请检查打印设置!", 16, "错误")         End Try     End Sub     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)         Dim row_count As Integer '当前要打印的行         PrintRecordLeave = DataTable1.Rows.Count - PrintRecordComplete '还有多少条记录没有打印         If PrintRecordLeave > 0 Then             If PrintRecordLeave Mod PrintRecordNumber = 0 Then                 PageNumber = PrintRecordLeave \ PrintRecordNumber             Else                 PageNumber = PrintRecordLeave \ PrintRecordNumber + 1             End If         Else             PageNumber = 0         End If         '正在打印的页数         PrintingPageNumber = 0       '因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0         '计算,余下的记录条数是否还可以在一页打印,不满一页时为假         If DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then             PageRecordNumber = PrintRecordNumber         Else             PageRecordNumber = (DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber         End If         Dim fmt As New StringFormat         fmt.LineAlignment = StringAlignment.Center '上下对齐         fmt.FormatFlags = StringFormatFlags.LineLimit '自动换行 


[解决办法]
答非所问
[解决办法]
这是vb.net的,对vb的语法不熟.下面的代码是用转换工具转出来的.你自己看看.有不对的自己改改

VB.NET code
 Public Function DGPrint(ByVal dg As DataGrid) As String            Dim rows As Integer =  dg.Items.Count             Dim col As Integer =  dg.Columns.Count             Dim sb As StringBuilder =  New StringBuilder()             String colHeaders = "<html><body>" +    "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"    Dim "<table border=1><tr>" As +            Dim i As Integer            For  i = 0 To  col- 1  Step  i + 1                colHeaders = "<td>" +dg.Columns(i).HeaderText.ToString().Trim() + "</td>"            Next            colHeaders += "</tr>"            sb.Append(colHeaders)             Dim i As Integer            For  i = 0 To  rows- 1  Step  i + 1                sb.Append("<tr>")                Dim j As Integer                For  j = 0 To  col- 1  Step  j + 1                    sb.Append("<td>")                    sb.Append(dg.Items(i).Cells(j).Text.ToString().Trim())                    sb.Append("</td>")                Next                sb.Append("</tr>")            Next            sb.Append("</table></body></html>")            colHeaders = sb.ToString()            colHeaders += "<script languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;window.close();</script>"            Return (colHeaders) End Function
[解决办法]
都是强人
以前我还一拿用友华表 用vbscript做打印太落伍了

热点排行