在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); }
}