作者:丁丽君coolboy | 来源:互联网 | 2023-09-24 19:04
话不多说,先看代码,一起学习与进步才是最大的财富。其实实现的技术难点不高,主要是画打印的表格花了我很多时间,如果用得上这个打印的表格模板大家也可以借鉴借鉴,或者有更好的可以分享给我。
1、打印的jsp:
<script type&#61;"text/Javascript">$(document).ready(function(){var items &#61; $("#items").val();var tdHeight &#61; $("#td").height();var totalHeight &#61; $("#trHeight").height();if(items>1){var endHeight &#61; totalHeight-(tdHeight*(items-1));$("#trHeight").css(&#39;height&#39;,endHeight);}setTimeout(&#39;execWB()&#39;,400);});function execWB(){document.all.WebBrowser.ExecWB(8,1);document.all.WebBrowser.ExecWB(6,1);}
script><style type&#61;"text/css">
.printcss_two{font-size: 20px;font-family: &#39;宋体&#39;;font-weight: bold;
}
.printTable{border: 1px solid #000;width:805px;height:350px;
}
.printTable1{border: 1px solid #000;;cellpadding:0;cellspacing:0;width:805px;
}
.printTable tr td{border: 1px solid #000;height:33px;font-size: 13px;font-family: &#39;宋体&#39;;
}
.printTable1 tr td{border: 1px solid #000;font-size: 13px;font-family: &#39;宋体&#39;;}
.tdcss1{font-size: 14px;font-family: &#39;宋体&#39;;font-weight: bold;height:35px;
}
.tdcss2{font-size: 13px;font-family: &#39;宋体&#39;;height:32px;}
.printcss_one{font-size: 18px;font-family: &#39;宋体&#39;;font-weight: bold;
}
style>
<style media&#61;print>.Noprint{display:none;}.PageNext{page-break-after:always;
}
style>
head>
<body class&#61;"PageNext">
<input type&#61;"hidden" id&#61;"items" value&#61;"${items}"/>
<object id&#61;WebBrowser classid&#61;CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height&#61;0 width&#61;0 VIEWASTEXT>object><div class&#61;"myPrintArea"align&#61;"center"><div class&#61;"printcss_one">XXXXXXXX表/br><font>打印实现font>div><table class&#61;"printTable"><tr><td align&#61;"left" width&#61;"155px" id&#61;"td">编号td><td align&#61;"left" width&#61;"155px">日期td><td align&#61;"left" width&#61;"212px">主管(${user.customsCode})td><td align&#61;"left" width&#61;"273px">单位(${user.tradeCo})td>tr><tr><td align&#61;"left" width&#61;"155px">${user.cargoNo}td><td align&#61;"left" width&#61;"155px"><fmt:formatDate value&#61;"${user.sendDate}" pattern&#61;"yyyy-MM-dd"/>td><td align&#61;"left" width&#61;"212px">${user.customsName}td><td align&#61;"left" width&#61;"273px">${user.tradeName}td>tr><tr><td align&#61;"left" width&#61;"155px">类型td><td align&#61;"left" width&#61;"155px">车号td><td align&#61;"left" width&#61;"212px">卡号td><td align&#61;"left" width&#61;"273px">关号td>tr><tr><td align&#61;"left" width&#61;"155px">${user.copeName}td><td align&#61;"left" width&#61;"155px">${user.ptsNo}td><td align&#61;"left" width&#61;"212px">${user.iarNo}td><td align&#61;"left" width&#61;"273px">${user.slNo}td>tr><tr><td align&#61;"left" width&#61;"155px">姓名td><td align&#61;"left" width&#61;"155px">帐号td><td align&#61;"left" width&#61;"212px">起地td><td align&#61;"left" width&#61;"273px">抵地td>tr><tr><td align&#61;"left" width&#61;"155px">${user.tame}td><td align&#61;"left" width&#61;"155px">${user.flyageNo}td><td align&#61;"left" width&#61;"212px">${user.inCode}td><td align&#61;"left" width&#61;"273px">${user.ivlCode}td>tr><tr><td align&#61;"left" width&#61;"155px">号1td><td align&#61;"left" width&#61;"155px">号2td><td align&#61;"left" width&#61;"212px">车重&#xff08;KG&#xff09;td><td align&#61;"left" width&#61;"273px">拖重&#xff08;KG&#xff09;td>tr><tr><td align&#61;"left" width&#61;"155px">${user.container1}td><td align&#61;"left" width&#61;"155px">${user.container2}td><td align&#61;"left" width&#61;"212px">${user.carWeight}td><td align&#61;"left" width&#61;"273px">${user.terWeight}td>tr> <tr><td align&#61;"left" width&#61;"155px">重量&#xff08;KG&#xff09;td><td align&#61;"left" width&#61;"155px">${user.container}td><td align&#61;"left" width&#61;"212px">td><td align&#61;"left" width&#61;"273px">td>tr><table class&#61;"printTable1"><tr><td align&#61;"left" style&#61;"border-right:1px solid white" class&#61;"tdcss1">序号td><td align&#61;"left" style&#61;"border-left:1px solid white;border-right:1px solid white" class&#61;"tdcss1">证号 td><td align&#61;"center" style&#61;"border-left:1px solid white;border-right:1px solid white" class&#61;"tdcss1">td><td align&#61;"center" style&#61;"border-left:1px solid white;" class&#61;"tdcss1">td>tr><c:forEach items&#61;"${enoList}" var&#61;"entryNos" varStatus&#61;"entryNo"><tr><td align&#61;"left" style&#61;"border-right:1px solid white" class&#61;"tdcss2">${entryNo.count}td><td align&#61;"left" style&#61;"border-left:1px solid white;border-right:1px solid white" class&#61;"tdcss2">${entryNos.GODOWO}td><td align&#61;"center" style&#61;"border-left:1px solid white;border-right:1px solid white" class&#61;"tdcss2">td><td align&#61;"center" style&#61;"border-left:1px solid white;"class&#61;"tdcss2">td>tr>c:forEach> table><table class&#61;"printTable"><tr rowspan &#61;"6" height&#61;"610px" id&#61;"trHeight"><td >td>tr>table> table>div>body>
function execWB(){document.all.WebBrowser.ExecWB(8,1);document.all.WebBrowser.ExecWB(6,1);}此处调用打印控件来实现打印功能&#xff0c;setTimeout(&#39;execWB()&#39;,400);来实现调用打印方法的时间控制。
"PageNext">
通过PageNext来实现打印分页功能&#xff0c;当循环到一定数量的数据时就会进行分页。
其他需要注意的是&#xff1a;弹出的打印设置&#xff0c;需要将其参数全部设为"空"&#xff0c;否则会将页码和标题等全部打印上去。
${entryNo.count}&#xff1a;得到forEach循环的次数&#xff0c;有时候很实用。
此打印主要针对简单的JSP实现&#xff0c;当然&#xff0c;有很多更强大的打印软件和插件也是不错的&#xff0c;有兴趣大家也可以去学习学习&#xff0c;交流交流。