现在的位置: 首页 > 综合 > 正文

关于ASP.NET页面打印技术的总结

2013年01月18日 ⁄ 综合 ⁄ 共 5452字 ⁄ 字号 评论关闭

 B/S结构导致了Web应用程序中打印的特殊性。

  
程序运行在浏览器中,打印机在本地,而文件确可能在服务器上,导致了打印控制不是很灵活。

  
格式如何控制和定制等,是我们开发中可能会面对的问题。

  打印文档的生成

  • 1、客户端脚本方式

  一般情况下,主要使用JS
可以分析源页面的内容,将欲打印的页面元素提取出来,实现打印。通过分析源文档的内容,可以生成打印目标文档。

  优点:客户端独立完成打印目标文档的生成,减轻服务器负荷;

  缺点:源文档的分析操作复杂,并且源文档中的打印内容要有约定。

  • 2、服务器端程序方式

  利用后台代码从数据库中读取打印源,生成打印目标文档。当的页面生成时,还应适当考虑使用CSS
来实现强制分页控制。

  优点:可以生成内容非常丰富的打印目标文档,目标文档的内容的可控性强。由于打印内容是从数据库中获取的,所以生成操作相对简单;

  缺点:服务器端负载比较大;

  页面设置

  
页面设置主要是指设置打印文档的页边距、页眉、页脚、纸张等内容。页面设置将直接影响到打印文档版面的生成效果,所以它和打印文档的生成有着密切的关系。比如:表格的行数、大小、位置、字体的大小等。

  现有的技术是利用IE6.0
内置的打印模板方式来控制页面设置,其可以对打印目标文档产生非常大的影响。打印模板可以控制页边距、页眉、页脚、奇偶页等内容,并可以将用户的设置取得,还可以将设置发送到服务器端。打印模板技术可以自定预览窗口和打印格式,最大限度地影响目标文档和打印效果。

IE直接打印

 

  
即直接调用window.print或者webrower控件的ExecWB方法来打印。

  
优点:方便快捷,客户端无需任何设置即可。

  
缺点:打印控制不是很灵活。如果直接调用

  window.print来打印页面,页面上别的元素也会被打印处理,页头页尾的格式也不好控制。

  
常用方法:大部分情况会把查询的结果绑定到DataGrid上来,然后打印DataGrid。这种情况的打印一般来说格式比较固定简单,确定后基本不会再作更改。所以可以采用IE直接打印。

  【实例代码】

  注:这是客户端通过window.print打印指定内容。这里定义sprnstreprnstr来指定内容

  执行代码:

<input type="button" name="print" value="预览并打印" onclick="preview()">

 

  如果直接使用window.print将打印页面上的所有内容,但是我们可以使用

<script language="Javascript">
function preview()
{
    bdhtml=window.document.body.innerHTML;
    sprnstr="<!--startprint-->";
    eprnstr="<!--endprint-->";
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML=prnhtml;
    window.print();
}
</script>
<!--省略部分代码-->
<form id="WebForm1" method="post" runat="server">
     
<center>本部分以上不被打印</center>
     
<!--startprint-->
     
<div align="center">
         
<asp:DataGrid id="dgShow" runat="server">
              
<!--省略部分代码-->
         
</asp:DataGrid>
     
</div>
     
<!--endprint-->
     
<center>本部分以下不被打印</center>
     
<div align="center">
         
<input type="button" name="print" value="预览并打印" onclick="preview()">
     
</div>
     
<style> @media Print { .Noprn { DISPLAY: none }}
     
</style>
     
<class="Noprn">不打印</p>
     
<table id="datagrid">
         
<tr>
              
<td>打印</td>
         
</tr>
     
</table>
     
<input class="Noprn" type="button" onclick="window.print()" value="print">
</form>

 

WebBrowser
控件技术

 

  
打印操作的实现

  此功能的实现主要是利用WebBrowser控件的函数接口来实现打印、打印预览(默认的)

  页面设置(默认的)

<object ID=‘WebBrowser1’ WIDTH=0 HEIGHT=0
CLASSID=‘CLSID:8856F961-340A-11D0-A96B-00C04FD705A2’>
//打印
WebBrowser1.ExecWB(6,1);
//打印设置
WebBrowser1.ExecWB(8,1);
//打印预览
WebBrowser1.ExecWB(7,1);
//直接打印
WebBrowser1.ExecWB(6,6);

//自定义类PrintClass
public string DGPrint(DataSet ds)
{
    
//DGPrint执行的功能:根据DataTable转换成对应的HTML对应的字符串
     DataTable myDataTable=new DataTable();
     myDataTable=ds.Tables[
0];
 
     
int myRow=myDataTable.Rows.Count; 
     
int myCol=myDataTable.Columns.Count;
 
     StringBuilder sb=
new StringBuilder(); 
 
     
string colHeaders="<html><body>"+"<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>" +"<table><tr>"
 
     
for(int i=0;i<myCol;i++)
     {     
         colHeaders +=
"<td>"+ myDataTable.Columns[i].ColumnName.ToString()+"</td>";
     }
     colHeaders += 
"</tr>";
     sb.Append(colHeaders);
 
     
for(int i=0;i<myRow;i++)
     {       
         sb.Append(
"<tr>");
         
for(int j=0;j<myCol;j++)
         {
              sb.Append(
"<td>");
              sb.Append(myDataTable.Rows[i][j].ToString().Trim());
              sb.Append(
"</td>");
         }
         sb.Append(
"</tr>");   
     }
 
     sb.Append(
"</table></body></html>"); 
     colHeaders=sb.ToString();               
     colHeaders+=
"<script languge='Javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>";
     
return(colHeaders);
}

 

  //页面:打印按钮事件

PrintClass myP = new PrintClass(); 
Response.Write(myP.DGPrint(Bind());

 

  在把DataGrid转换为对应的HTML代码时,如果存在按钮列就会报错,最好把这一列隐藏,一般只能转换数据列。其次要注意分页问题,一般只能打印当前一页,最好在打印之前除掉分页

导出到ExcelWord中去打印

 

  
可以在服务端或者客户端进行。

  
优点:使用这种方法,可适应性比较强,控制较好。

  
缺点:在服务端使用的话,要求服务端要安装WordExcel,在客户端使用的话,要

  求客户端在IE的安全设置上有一定要求。

  代码如下:

protected void btnMIME_Click(object sender, System.EventArgs e)
{
     BindData();
 
     Response.ContentType = 
"application/vnd.ms-excel";
     Response.AddHeader(
"Content-Disposition""inline;filename="+HttpUtility.UrlEncode("下载文件.xls",Encoding.UTF8));        
 
     
//如果输出为Word,修改为以下代码
     //Response.ContentType = "application/ms-word" 
     //Response.AddHeader("Content-Disposition", "inline;filename=test.doc") 
     StringBuilder sb=new StringBuilder(); 
     System.IO.StringWriter sw = 
new System.IO.StringWriter(sb);
     System.Web.UI.HtmlTextWriter hw = 
new System.Web.UI.HtmlTextWriter(sw);
     sb.Append(
"<html><body>");
     dgShow.RenderControl(hw);
     sb.Append(
"</body></html>"); 
     Response.Write(sb.ToString());
     Response.End();
}
 
protected void btnCom_Click(object sender, System.EventArgs e)
{
     ExportToExcel(BindData(),Server.MapPath(
"ComExcel.xls"));
 
}
//DataSet到出到Excel
#regionDataSet到出到Excel
///导出指定的Excel文件
public void ExportToExcel(DataSet ds,string strExcelFileName)
{
     
if (ds.Tables.Count==0 || strExcelFileName==""return;
     doExport(ds,strExcelFileName);
}
///执行导出
private void doExport(DataSet ds,string strExcelFileName)
{
     excel.Application excel= 
new excel.Application();
     
int rowIndex=1;
     
int colIndex=0;
     excel.Application.Workbooks.Add(
true);
     System.Data.DataTable table=ds.Tables[
0] ;
     
foreach(DataColumn col in table.Columns)
     {
         colIndex++;    
         excel.Cells[
1,colIndex]=col.ColumnName;                
     }
 
     
foreach(DataRow row in table.Rows)
     {
         rowIndex++;
         colIndex=
0;
         
foreach(DataColumn col 

抱歉!评论已关闭.