c# 网页打印全流程

时间:2021-11-18 03:38:41

说明:我要实现的就是将数据库中Group表的数据查找出来,替换打印模版中的内容,再将模版文件打印出来

1、准备好要打印的模版group_O_train.html

1 <div class=http://www.mamicode.com/"pagetitle title">出国(境)培训人员汇总表</div> 2 <div>项目编号:{项目编号}</div> 3 <table class=http://www.mamicode.com/"pagedtable" style=http://www.mamicode.com/"width:25cm;font-size:9pt;border-collapse:collapse;" cellspacing=http://www.mamicode.com/0 cellpadding=3px border=http://www.mamicode.com/1 bordercolor=black> 4 <thead style=http://www.mamicode.com/"text-align:center"> 5 <tr> 6 <td style=http://www.mamicode.com/"width:0.6cm">序号</td> 7 <td style=http://www.mamicode.com/"width:1.5cm">姓 名</td> 8 <td style=http://www.mamicode.com/"width:0.8cm">性别</td> 9 <td style=http://www.mamicode.com/"width:2.0cm">出 生<br />年月日</td> 10 <td style=http://www.mamicode.com/"width:1.5cm">出生地</td> 11 <td style=http://www.mamicode.com/"width:1.0cm">政治面貌</td> 12 <td style=http://www.mamicode.com/"width:4.5cm">工作单位</td> 13 <td style=http://www.mamicode.com/"width:2.7cm">职务或<br />职 称</td> 14 <td style=http://www.mamicode.com/"width:4cm">何年毕业于<br />何校、何专业</td> 15 <td style=http://www.mamicode.com/"width:2cm">目前从事的<br />实际工作</td> 16 <td style=http://www.mamicode.com/"width:1.0cm">备注</td> 17 </tr> 18 </thead> 19 <tbody> 20 {人员名单} 21 </tbody> 22 </table> 23 <p class=http://www.mamicode.com/"alignright" style=http://www.mamicode.com/"width:25cm">出国(境)归口管理部门公章</p>

View Code

2、打印通用页面,其中<div runat="server">是模版要替换的内容

1 <%@ Page Language=http://www.mamicode.com/"C#" AutoEventWireup=http://www.mamicode.com/"true" CodeFile=http://www.mamicode.com/"DocumentPrint.aspx.cs" Inherits=http://www.mamicode.com/"Abroad_DocumentPrint" EnableViewState=http://www.mamicode.com/"false" %> 2 3 <html xmlns=http://www.mamicode.com/"" > 4 <head runat=http://www.mamicode.com/"server"> 5 <meta http-equiv=http://www.mamicode.com/"X-UA-Compatible" content=http://www.mamicode.com/"IE=edge,chrome=1" /> 6 7 <title>打印</title> 8 <link href=http://www.mamicode.com/"../css/print.css" rel=http://www.mamicode.com/"stylesheet" type=http://www.mamicode.com/"text/css" /> 9 </head> 10 <body onload=http://www.mamicode.com/"PageSetup_Null()"> 11 <form id=http://www.mamicode.com/"form1" runat=http://www.mamicode.com/"server"> 12 <object id=http://www.mamicode.com/"WebBrowser" width=http://www.mamicode.com/0 height=http://www.mamicode.com/0 classid=http://www.mamicode.com/"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"> 13 </object> 14 <div class=http://www.mamicode.com/"printbar"> 15 <div style=http://www.mamicode.com/"float:right"> 16 <a href=http://www.mamicode.com/"javascript:void(0)" onclick=http://www.mamicode.com/"print()"><img src=http://www.mamicode.com/"../images/printbutton.gif" border=http://www.mamicode.com/0 / alt=http://www.mamicode.com/"打印"></a> 17 <a href=http://www.mamicode.com/"javascript:void(0)" onclick=http://www.mamicode.com/"printtrainmember()" runat=http://www.mamicode.com/"server" id=http://www.mamicode.com/"btprinttmember"><img src=http://www.mamicode.com/"../images/printbutton.gif" border=http://www.mamicode.com/0 / alt=http://www.mamicode.com/"打印培训人员" ></a> 18 &nbsp;&nbsp;<a href=http://www.mamicode.com/"javascript:void(0)" onclick=http://www.mamicode.com/"printPreview()" ><img src=http://www.mamicode.com/"../images/printpreviewbutton.gif" border=http://www.mamicode.com/0 / alt=http://www.mamicode.com/"打印预览"></a> 19 &nbsp;&nbsp;<a href=http://www.mamicode.com/"javascript:void(0)" onclick=http://www.mamicode.com/"window.close()"><img src=http://www.mamicode.com/"../images/closebutton.gif" border=http://www.mamicode.com/0 / alt=http://www.mamicode.com/"打印"></a> 20 </div> 21 </div> 22 <div id=http://www.mamicode.com/"pPrintContent" runat=http://www.mamicode.com/"server"> 23 错误,没有内容!请检查调用参数是否正确. 24 </div> 25 </form> 26 </body> 27 <script src=http://www.mamicode.com/"../js/print.js" type=http://www.mamicode.com/"text/javascript"></script> 28 <script type=http://www.mamicode.com/"text/javascript"> 29 var HKEY_Root,HKEY_Path,HKEY_Key; 30 HKEY_Root=http://www.mamicode.com/"HKEY_CURRENT_USER"; 31 HKEY_Path=http://www.mamicode.com/"\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; 32 //设置网页打印的页眉页脚为空 33 function PageSetup_Null() 34 { 35 try 36 { 37 var Wsh=http://www.mamicode.com/new ActiveXObject("WScript.Shell"); 38 39 HKEY_Key=http://www.mamicode.com/"header"; 40 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 41 HKEY_Key=http://www.mamicode.com/"footer"; 42 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 43 44 HKEY_Key=http://www.mamicode.com/"margin_left" 45 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,0); 46 47 HKEY_Key=http://www.mamicode.com/"margin_top" 48 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,0); 49 50 HKEY_Key=http://www.mamicode.com/"margin_right" 51 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,0); 52 53 HKEY_Key=http://www.mamicode.com/"margin_bottom" 54 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,0); 55 56 HKEY_Key=http://www.mamicode.com/"orientation" 57 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,2); 58 59 } 60 catch(e) 61 {} 62 } 63 </script> 64 </html>

View Code

3、在你所要打印的页面加上打印的按钮,