众所周知,WEB上的打印是比较困难的,常见的WEB上打印的方法大概有三种:
1、直接利用IE的打印功能。一般来说,这种方法可以做些扩展,而不是单单的调用javascript:print()这样简单,比如,可以使用如下代码:
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0> </OBJECT> <input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)> <input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6)> <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)> <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1)> |
Imports System.Runtime.InteropServices.Marshal Imports Office Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '以COM方式处理Excel Dim oExcel As New Excel.Application Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet Dim oCells As Excel.Range Dim sFile As String, sTemplate As String '定义一个datatable Dim dt As DataTable = CType(Application.Item("MyDataTable"), DataTable) sFile = Server.MapPath(Request.ApplicationPath) & "\MyExcel.xls" '定义模版文件 sTemplate = Server.MapPath(Request.ApplicationPath) & "\MyTemplate.xls" oExcel.Visible = False oExcel.DisplayAlerts = False '定义一个新的工作簿 oBooks = oExcel.Workbooks oBooks.Open(Server.MapPath(Request.ApplicationPath) & "\MyTemplate.xls") oBook = oBooks.Item(1) oSheets = oBook.Worksheets oSheet = CType(oSheets.Item(1), Excel.Worksheet) '命名该sheet oSheet.Name = "First Sheet" oCells = oSheet.Cells '调用dumpdata过程,将数据导入到Excel中去 DumpData(dt, oCells) '保存 oSheet.SaveAs(sFile) oBook.Close() '退出Excel,并且释放调用的COM资源 oExcel.Quit() ReleaseComObject(oCells) : ReleaseComObject(oSheet) ReleaseComObject(oSheets) : ReleaseComObject(oBook) ReleaseComObject(oBooks) : ReleaseComObject(oExcel) oExcel = Nothing : oBooks = Nothing : oBook = Nothing oSheets = Nothing : oSheet = Nothing : oCells = Nothing System.GC.Collect() Response.Redirect(sFile) End Sub '将DATATABLE的内容导出到Excel的单元格中去 Private Function DumpData(ByVal dt As DataTable, ByVal oCells As Excel.Range) As String Dim dr As DataRow, ary() As Object Dim iRow As Integer, iCol As Integer '输出列标题 For iCol = 0 To dt.Columns.Count - 1 oCells(2, iCol + 1) = dt.Columns(iCol).ToString Next '将数据导出到相应的单元格 For iRow = 0 To dt.Rows.Count - 1 dr = dt.Rows.Item(iRow) ary = dr.ItemArray For iCol = 0 To UBound(ary) oCells(iRow + 3, iCol + 1) = ary(iCol).ToString Response.Write(ary(iCol).ToString & vbTab) Next Next End Function End Class |
|
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dt As DataTable = CType(Application.Item("MyDataTable"), DataTable) Response.ContentType = "application/ms-Excel" Response.AddHeader("Content-Disposition", "inline;filename=test.xls") Response.Write(ConvertDtToTDF(dt)) End Sub Private Function ConvertDtToTDF(ByVal dt As DataTable) As String Dim dr As DataRow, ary() As Object, i As Integer Dim iCol As Integer ' 输出列标题 For iCol = 0 To dt.Columns.Count - 1 Response.Write(dt.Columns(iCol).ToString & vbTab) Next Response.Write(vbCrLf) '输出数据 For Each dr In dt.Rows ary = dr.ItemArray For i = 0 To UBound(ary) Response.Write(ary(i).ToString & vbTab) Next Response.Write(vbCrLf) Next End Function End Class |
|
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) Dim dt As DataTable = CType(Application.Item("MyDataTable"), DataTable) Response.ContentType = "application/ms-Excel" Response.AddHeader("Content-Disposition", "inline;filename=test.xls") DataGrid1.DataSource = dt DataGrid1.DataBind() DataGrid1.RenderControl(writer) End Sub |
|
Response.ContentType = "application/ms-word" Response.AddHeader("Content-Disposition", "inline;filename=test.doc") |
<script language="vbscript"> Sub exportbutton_onclick Dim sHTML, oExcel, oBook SHTML = document.all.item("DataGrid1").outerhtml Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.Add oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML oBook.HTMLProject.RefreshDocument oExcel.Visible = true oExcel.UserControl = true End Sub </script> |
Dim dt As DataTable = CType(Application.Item("MyDataTable"), DataTable) DataGrid1.DataSource = dt DataGrid1.DataBind() |