在web系统设计中经常需要进行表格打印,为了省事采用WebBrowser来实现,参考网上资料,个人总结如下
1、显示打印页面的定义
<style media="print" type="text/css">
<%--打印专用式样--%>
.Noprint{
display:none;
}
.PageNext{page-break-after: always;}
.printTable{font-size:12px; font-weight:bold; text-align:center;height: 310px;}
</style>
<script src="Scripts/my_print.js"></script>
<style type="text/css">
.AutoNewline {word-wrap: break-word; word-break: normal;}
</style>
</head>
<body>
<div class="Noprint" align="center" > <%--不打印的区域--%>
<object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0">
</object>
<input type="button" class="an" οnclick="doPrint();" value="打印"/>
<input type="button" class="an" οnclick="Pageset();" value="页面设置"/>
<input type="button" class="an" value="打印预览" οnclick="Printview()"/>
</div>
<%-- 下面打印内容区--%>
<div id="disp">
<%=retutnPrintHtml %>
<%--<div class="PageNext"></div> 需要换行的地方加上此句--%>
</div>
打印脚本部分,需要具有较高权限
// JavaScript Document
//下面代码可以采用注册表备份文件导入reg,也可以采用手工修改Ie的打印页面的设置
//25.4毫米=1英寸
var HKEY_Root,HKEY_Path,HKEY_Key;
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
//设置网页打印的页眉页脚为空
PageSetup(0.1,0.1,0.1,0.1) ;
function PageSetup(margin_left,margin_right,margin_top,margin_bottom) {
try
{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="margin_bottom";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,margin_bottom);
HKEY_Key="margin_left";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,margin_left);
HKEY_Key="margin_right";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,margin_right);
HKEY_Key="margin_top";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,margin_top);
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="Shrink_To_Fit";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"yes");
}
catch(e){
//alert(e);
}
}
function doPrint()
{
document.all.WebBrowser.ExecWB(6,6);
window.opener=null;
window.close();
}
function Printview()
{document.all.WebBrowser.ExecWB(7,1)
}
function Pageset()
{
document.all.WebBrowser.ExecWB(8,1)
}
function printsNow() {
if (confirm('确定打印吗?')) { wb.execwb(6,1); }
}