天天看點

html或jsp實作列印三種方法

1.使用window.print()方法

優點:支援多浏覽器

缺點:取消列印,隐藏列印不必要的資訊後再顯示比較麻煩

如下實作,可以列印目前頁面

<input name ="Button"  value="火狐谷歌列印" type="button" οnclick="window.print()" />
           

2.使用第三方插件的功能進行列印,eg:ireport報表插件 優點:支援多浏覽器 缺點:實作起來比較麻煩

3.調用ie内置列印控件IEWebBrowser實作列印功能 優點:針對ie功能強大 缺點:隻支援ie

<%@ page language ="java" import="java.util.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort()+path+ "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
       <head>
             <base href =" <%=basePath%> ">
             <title> My JSP 'print.jsp' starting page</title >
             <meta http-equiv ="pragma" content="no-cache" >
             <meta http-equiv ="cache-control" content="no-cache" >
             <meta http-equiv ="expires" content="0" >
             <meta http-equiv ="keywords" content="keyword1,keyword2,keyword3" >
             <meta http-equiv ="description" content="This is my page" >
             <!-- 列印時下列方法生效 -->
             <style media ="print" >
            <!--
            . Noprint{display: none;}
            .PageN ext{pag e-break-after:always;}
            -->
             </style>
             <style type ="text/css" >
             <!--
             .STYLE1 {font-size : 12px }
             -->
             </style>
       </head>

       <body>
             <br>
             <table border ='8' width="500" height="500" >
                   <tr> <td>haha</td><td>haha</td><td>haha</td><td>haha</td></tr>
                   <tr><td></ td><td></ td><td></ td><td></ td></tr>
                   <tr><td></ td><td></ td><td></ td><td></ 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) class="Noprint" type=button value=打開 />
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(2,1) class="Noprint" type=button value=關閉所有 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(4,1) class="Noprint" type=button value=另存為 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(6,1) class="Noprint" type=button value=列印 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(6,6) class="Noprint" type=button value=直接列印 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(7,1) class="Noprint" type=button value=列印預覽 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(8,1) class="Noprint" type=button value=頁面設定 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(10,1) class="Noprint" type=button value=屬性 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(17,1) class="Noprint"       type=button value=全選 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(22,1) class="Noprint"       type=button value=重新整理 >
                   <input name =Button onClick=document.all.WebBrowser.ExecWB(45,1) class="Noprint" type=button value=關閉 >
             </p>
       </body>
</html>