Asp.net 网络打印 – WebBrowser 控件技术打印
Asp.net 网络打印 – WebBrowser 控件技术打印
---资源来源于Microsfot WebCast
1. 创建一个Class_Print 的类,类中可能创建两个Function.
-Function 1:
Public Function DGPrint(ByVal ds As DataSet) As String
Dim myDataTable As New DataTable
myDataTable = ds.Tables(0)
Dim myRow As Integer = myDataTable.Rows.Count
Dim myCol As Integer = myDataTable.Columns.Count
Dim sb As New System.Text.StringBuilder
Dim colHeaders As String = "<html><body><object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object><table><tr>"
For lint_index_0 As Integer = 0 To myCol - 1
colHeaders += "<td>" + myDataTable.Columns(lint_index_0).ColumnName.ToString() + "</td>"
Next
colHeaders += "<tr>"
sb.Append(colHeaders)
For lint_index_1 As Integer = 0 To myRow - 1
sb.Append("<tr>")
For lint_index_2 As Integer = 0 To myCol - 1
sb.Append("<td>")
sb.Append(myDataTable.Rows(lint_index_1).Item(lint_index_2).ToString.Trim)
sb.Append("</td>")
Next
sb.Append("</tr>")
Next
sb.Append("</table></body></html>")
colHeaders = sb.ToString
'// WebBrowser.ExecWB(6,1) ----打印
'// WebBrowser.ExecWB(8,1) ----打印设置
'// WebBrowser.ExecWB(7,1) ----打印预览
'// WebBrowser.ExecWB(6,6) ----直接打印
colHeaders += "<script language='javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>"
Return colHeaders
End Function
-Function 2
Public Function DGPrint(ByVal dg As DataGrid) As String
Dim myRow As Integer = dg.Items.Count
Dim myCol As Integer = dg.Columns.Count
Dim sb As New System.Text.StringBuilder
Dim colHeaders As String = "<html><body><object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object><table><tr>"
For lint_index_0 As Integer = 0 To myCol - 1
colHeaders += "<td>" + dg.Columns(lint_index_0).HeaderText.ToString() + "</td>"
Next
colHeaders += "<tr>"
sb.Append(colHeaders)
For lint_index_1 As Integer = 0 To myRow - 1
sb.Append("<tr>")
For lint_index_2 As Integer = 0 To myCol - 1
sb.Append("<td>")
sb.Append(dg.Items(lint_index_1).Cells(lint_index_2).Text.Trim())
sb.Append("</td>")
Next
sb.Append("</tr>")
Next
sb.Append("</table></body></html>")
colHeaders = sb.ToString
'// WebBrowser.ExecWB(6,1) ----打印
'// WebBrowser.ExecWB(8,1) ----打印设置
'// WebBrowser.ExecWB(7,1) ----打印预览
'// WebBrowser.ExecWB(6,6) ----直接打印
colHeaders += "<script language='javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>"
Return colHeaders
End Function
2. Web 页面的打印调用:
Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click
Dim iobj_print As New PrintClass
Dim ds As New DataSet
ds.Tables.Add(idtb_temp)
Response.Write(iobj_print.DGPrint(ds))
End Sub