直接在jsp页面实现打印,需要启用IE的ActiveX控件。
实现打印其实不是jsp的功能,而是浏览器的功能,我们只是通过js直接调用了浏览器的打印功能了:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- 需要启用IE的 自定义级别中的 没有标记为安全的ActiveX控件 -->
<!--
好用,使用的时候注意要创建浏览器对象<OBJECTid="WebBrowser" ></OBJECTid>,而且要注意html页面的dtd是否正确:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSP中实现打印功能</title>
<style type="text/css">
<!--
.STYLE5 {color: #993399; font-size: xx-large; }
.STYLE6 {font-size: xx-large}
-->
</style>
<!-- 将不用打印出来的地方隐藏了-->
<style type="text/css" media="print">
<!--
.dis{display:none;}
-->
</style>
</head>
<body>
<table width="616" height="350" border="1" align="center">
<tr>
<td colspan="2"> <span class="STYLE6">bbbbb </span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbbb</span></td>
</tr>
</table>
<p class="dis" align="center">
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id="WebBrowser" width=0> </OBJECT>
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性>
<input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全选>
<input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
<input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=关闭>
</p>
</body>
</html>