我想打印出JSP页面显示的所有记录,如:查询出数据记录是200条,分20页显示,大家有没有好的办法可以点击“button”便可打印这200条记录呀?即可以实现翻页打印的效果...目前用到的方法都只能打印当前页。                                                    谢谢各位提出好的建议

解决方案 »

  1.   


    <title>JS 分页批量打印解决方案(只支持IE)</title>  <script language="javascript">  <!--  function printWithAlert() {  document.all.WebBrowser.ExecWB(6,1);  }  function printWithoutAlert() {  document.all.WebBrowser.ExecWB(6,6);  }  function printSetup() {  document.all.WebBrowser.ExecWB(8,1);  }  function printPrieview() {  document.all.WebBrowser.ExecWB(7,1);  }  function printImmediately() {  document.all.WebBrowser.ExecWB(6,6);  window.close();  }  -->   </script>  <!-- 此控件用来直接调用浏览器的一些方法 -->  <OBJECT  id=WebBrowser  classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 style="display:none">  </OBJECT>  <!-- 适用于打印机的样式设置 -->  <style media=print>  .Noprint{display:none;}  .PageNext{page-break-after: always;}  </style>  <table align="center" class=NOPRINT>  <tr>  <td align="center">  <BUTTON title=打印 onclick=printWithAlert()>打印<IMG src="/UpFiles/server/2009-07/2009767388408342.gif" align=absmiddle></BUTTON>  <BUTTON title=直接打印 onclick=printWithoutAlert()>直接打印<IMG src="/UpFiles/server/2009-07/2009767388408342.gif" align=absmiddle></BUTTON>  <input type=button value="打印设置" onclick="printSetup()" >  <button onclick ='printPrieview()' title='打印预览...' >打印预览<img src='/UpFiles/server/2009-07/2009766915529838.gif'  align=absmiddle></button>  </td>  </tr>   </table>  <div  class=NOPRINT> 以下为打印区:<br></div>  <hr size=1 noshadow color=black  class=NOPRINT >  <table class="PageNext" >  <tr><td colspan=2>第一页数据</td></tr>  <tr><td>用户名</td><td>地址</td></tr>  <tr><td>张三</td><td>北京</td></tr>  </table>  <hr size=1 noshadow color=black  class=NOPRINT >  <div class="PageNext">  第二页数据  </div>  <hr size=1 noshadow color=black  class=NOPRINT >  第三页数据或者表格的数据应该是从数据库里面提取出来的吧,即使不是,统计其长度应该没有问题的,比如你是用vector存放它,通过vector.size取其数据长度。估计表头,表尾占去长度,不妨为5行,设定每页显示表格55行(具体看你的情况,若表格过于紧密会造成一行记录打印成两行)。设定分页相关参数  
                  //for   print  
                  int   iPageSize   =   55;       //每页设定表格行数  
                  int   iPageCount   =   0   ;     //第n页数  
                  int   iLastPage   =   0   ;       //最后一页  
                  int   iRecordCount   =   0   ;   //表格总数  
                  int   iCount   =   0   ;             //具体表格行数  
                  int   iCurPage   =   0;         //当前页数  
      在jsp里面,先显示表头,比如  
                                                  ××××报表  
        打印日期:×××                               打印地点:×××××  
       
      按条显示表格记录  
      <%  
      for   (int   i=0;   i<iRecordCount;   i++)   {  
          if(iCount%iPageSize==0   &&   iCount!=0)   {//第n页第一条记录时  
      //重写表头信息,如上  
      //显示记录  
      ………………  
          }  
          iCount++;  
          //为整页时  
          if((iCount)%iPageSize==0   ||   iCount==iRecordCount)   {  
          //是本页或者最后一页最后一项  
              if(iCount==iRecordCount)   {  
                  //最后一页的表尾信息  
      ---------------------------------  
      依照需要写每页页尾信息,比如:  
          <div   align=center>  
          <%  
              //每页页尾信息  
              if(iCount==iRecordCount)   iCurPage=iPageCount;  
              else   iCurPage=iCount/iPageSize;  
          %>  
          第  
          <%=iCurPage%>  
          /  
          <%=iPageCount%>  
          页  
          </div>  
      ---------------------------------  
      }  
      %>   
      

  2.   

    点击全部打印按钮时,在把本页面在新的窗口打开,并吧把所有记录一页显示,然后自己调用打印方法。打印完毕后自己关闭。
    如下是个例子:<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <%@ page import="java.util.*"%>                
    <%@ include file="../System/ConstDefine.jsp"%>
    <%@ include file="../System/FunDefine.jsp"%>
    <%@ include file="../System/VarDefineList.jsp"%>
    <jsp:useBean   id="dgoa" scope="request"  class="beartool.MFSale_Table"/>
    <jsp:useBean   id="nongzi" scope="request"  class="beartool.Sales_CreditNotesFunction"/><jsp:useBean   id="nongzi2" scope="request"  class="beartool.M_Rz_SCFunction"/><%
    //String[][] fffff;
    //fffff=nongzi2.GetRzGridSet(DBName,sysid);
    String time1=(String)request.getParameter("time1");//开始时间
    String time2=(String)request.getParameter("time2");//结束时间
    String Type=codeToString((String)request.getParameter("Type"));String biaoshi=(String)request.getParameter("biaoshi");String[][] AA;
    AA=nongzi.GetCreditNotesQuery2(DBName,sysid,Type,time1,time2);
    int n=0;
    int m2=0;float sum=Float.parseFloat("0");
    float sum1=0;
    float sum2=0;
    float sum3=0;
    float sum4=0;
    float sum5=0;
    float sum6=0;
    szPageBarURL="Sales_CreditNotesReportTable.jsp?Type="+Type+"&time1="+time1+"&time2="+time2+"&PageNo=";%>
    <%
    nAddButtonStyle = 1;
    if(biaoshi==null||biaoshi=="")
    {nPageSize = 20;}
    else if(biaoshi.equals("1"))
    {nPageSize = AA.length;
    }%>
    <%@ include file="../System/PowerList.jsp"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title></title>
    <link rel="stylesheet" href="../Common/Main.css">
    <script language="javascript" SRC="../Common/FunDefine.js"></script>
    <link rel="stylesheet" href="../Common/Extra.css">
    <script language='javascript' src='../Common/graspcrmcore.js'></script>
    <LINK href="../Common/style.css" rel=stylesheet>
    <SCRIPT language=javascript src="../Common/calendar.js"></SCRIPT>
    <script LANGUAGE="javascript">function pass(){
      
     window.open('Sales_CreditNotesReportTable.jsp?time1=<%=time1%>&time2=<%=time2%>&Type=<%=Type%>&biaoshi=1','','top=0,left=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height='+ new String(screen.availHeight-28) + ',width=' + new String(screen.availWidth-10));

     } function prn1_preview() {
    CreateOneFormPage();
    LODOP.PREVIEW();
    };
    function prn1_print() {
    CreateOneFormPage();
    LODOP.PRINT();
    };
    function prn1_printA() {
    CreateOneFormPage();
    LODOP.PRINTA(); 
    };
    function CreateOneFormPage(){
     
    LODOP.SET_PRINT_TEXT_STYLE(1,"宋体",18,1,0,0,1);
    LODOP.ADD_PRINT_HTM(0,0,760,1128,document.getElementById("form1").innerHTML);
    LODOP.SET_PRINT_PAGESIZE(0,0,2970,"A4");
    };printform=function(){
    // prn1_preview() ;
     prn1_print();
    window.opener=null;
    window.open("","_self");
    window.close(); 
     }
    function printit(Id) { 
      if (confirm('确定打印吗?')==true) { 
           var   mStr;   
           mStr   =   window.document.body.innerHTML   ;   
           var   mWindow   =   window;   
           window.document.body.innerHTML   =Id.innerHTML;     
           mWindow.print();   
           window.document.body.innerHTML   =   mStr;   
         //wb.execwb(6,6) 
       }
    }
    function changeTwoDecimal_f(x)
    {
       var f_x = parseFloat(x);
       if (isNaN(f_x))
       {
          alert('function:changeTwoDecimal->parameter error');
          return false;
       }
       var f_x = Math.round(x*100)/100;
       var s_x = f_x.toString();
       var pos_decimal = s_x.indexOf('.');
       if (pos_decimal < 0)
       {
          pos_decimal = s_x.length;
          s_x += '.';
       }
       while (s_x.length <= pos_decimal + 2)
       {
          s_x += '0';
       }
       return s_x;
    }
    function FormatNumber(srcStr, nAfterDot){//小数点后的位数
    var srcStr,nAfterDot;
    var resultStr,nTen;
    srcStr = ""+srcStr+"";
    strLen = srcStr.length;
    dotPos = srcStr.indexOf(".",0);
    if (dotPos == -1){
       resultStr = srcStr+".";
       for (i=0;i<nAfterDot;i++){
        resultStr = resultStr+"0";
       }
    }else{
       if ((strLen - dotPos - 1) >= nAfterDot){
        nAfter = dotPos + nAfterDot + 1;
        nTen =1;
        for(j=0;j<nAfterDot;j++){
         nTen = nTen*10;
        }
        resultStr = Math.round(parseFloat(srcStr)*nTen)/nTen;
       }else{
        resultStr = srcStr;
        for (i=0;i<(nAfterDot - strLen + dotPos + 1);i++){
         resultStr = resultStr+"0";
        }
       }
    }
    return resultStr;

    function setCurrency(s){//金额显示格式
        if(/[^0-9\.\-]/.test(s)) return " Wrong Number!";
        s=s.replace(/^(\d*)$/,"$1.");    s=(s+"0000").replace(/(\d*\.\d\d)\d*/,"$1");//取小数点后两位
        s=s.replace(".",",");
        var re=/(\d)(\d{3},)/;
        while(re.test(s))
            s=s.replace(re,"$1,$2");
        s=s.replace(/,(\d\d)$/,".$1");//取小数点后两位
      
        return "¥"+s.replace(/^\./,"0.")
        }
     </script>
    </head>
    <body <%=BODY%>>
    <form id="form1"><table width="100%"  border="0" style="color:#000000">
       <tr>
           <td ><div align="center" style="font-size:15pt;font-weight:700;color:#666666;">--<%=Gs_Name%>--</div></td>
       </tr>
       <tr>
           <td>&nbsp;</td>
       </tr>
       <tr>    <td ><div align="center" style="font-size:18pt;font-weight:700;text-decoration:underline;color:#000000">欠 条 统 计 报 表</div></td>
       </tr>
       <tr>
           <td align="center"><当前组织机构:<%=Acc_OrgName%>;业务机构:<%=BussinesName%> ></td>
       </tr>
       <tr>
    <td colspan="3" align="center">  <font size="2"> <%=time1%>年<%=time2%>月 </td>
                                           
        </tr>   
    </table><br>
    <tr><td colspan="2">
    <table  align="center"bordercolor="#000000" border="1" cellpadding="0" cellspacing="0" style="width:740px;border:1px solid #000000;border-collapse:collapse" >
    <%if(AA.length==0)
        {%>
    <tr >
        <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000"align="center" height="25" width="39"> 序号</td>
        <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" >单号</td> 
    <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000"  align="center" height="25" > 欠款类型</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 欠款单位</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 现金银行</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 欠款人</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 顺序号</td>
        <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" >欠款日期</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 预还款日期</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 经手人</td>
    <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000" align="center" height="25"  > 欠款金额 </td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 还款金额</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 未还款金额</td>
    </tr> <tr class="ListTableRow">
    <td colspan="13" align="center" height="25">&nbsp;没有数据!</td>
    </tr>
       <%}else{%>
    <%int i=0;
      int j=0;
         int t=0;
      while(t<AA.length){
         String huankuan1=nongzi.GetCreditRebackM2(DBName,sysid,AA[t][0]);
     float huankuan=Float.parseFloat(huankuan1);
     float qiankuan=Float.parseFloat(AA[t][17]);
     float weihuankuan=qiankuan-huankuan;
     sum4=sum4+qiankuan;
     sum5=sum5+huankuan;
     sum6=sum6+weihuankuan;
    t++;
      }
     
       nRecordCount=0;
    nStartLine = nPageSize * (nPageNo - 1);
    nEndLine = nPageSize * nPageNo - 1;
    if(biaoshi==null||biaoshi=="")
    {nStartLine = nPageSize * (nPageNo - 1);
    nEndLine = nPageSize * nPageNo - 1;}
    else if(biaoshi.equals("1"))
    {nStartLine = 0;
    nEndLine = AA.length+1;
    }

    nRecordCount=AA.length;
        n=0;
    while((nStartLine+n++)<=nEndLine){
    m2=(int)nStartLine+n-1;
      

  3.   

    接上面:

     if(m2<AA.length){
    String huankuan1=nongzi.GetCreditRebackM2(DBName,sysid,AA[m2][0]);
     float huankuan=Float.parseFloat(huankuan1);
     float qiankuan=Float.parseFloat(AA[m2][17]);
     float weihuankuan=qiankuan-huankuan;
     sum1=sum1+qiankuan;
     sum2=sum2+huankuan;
     sum3=sum3+weihuankuan;
    /* int yuji2 = Integer.parseInt(AA[m2][11]);
      sum3=sum3+yuji2;
     int leiji2 = Integer.parseInt(AA[m2][8]);
      sum4=sum4+leiji2;
    */
     
     %>
      <tr style="font-weight:bold">
    <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000"align="center" height="25" width="39"> 序号</td>
        <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" >单号</td> 
    <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000"  align="center" height="25" > 欠款类型</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 欠款单位</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 现金银行</td>

    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 欠款人</td>
    <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 顺序号</td>
    </tr>
    <tr>
     <%
     int x=(int)nPageNo;  if(nPageNo>1)
     {
     j++;
     i = (x-1)*20+j;
     %>
    <td nowrap  align="center" rowspan="3" ><%=i%></td>
    <%}else{
     i++;  %>
      <td nowrap align="center" rowspan="3" ><%=i%></td> <%}%>

     
    <td nowrap  align="center" height="25" > <%=AA[m2][0]%></td> 
    <td nowrap  align="center" height="25" ><%=AA[m2][9]%> </td>
    <td nowrap  align="center" height="25" > <%=AA[m2][5]%></td>
    <td  nowrap  align="center" height="25"> <%=AA[m2][19]%></td>  
    <td  nowrap  align="center" height="25"> <%=AA[m2][6]%></td>
    <td  nowrap  align="center" height="25"> <%=AA[m2][12]%></td>
    </tr>
    <tr style="font-weight:bold"> <td  height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" >欠款日期</td>


    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 预还款日期</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 经手人</td>
    <td nowrap  bgcolor="#FFFFFF" bordercolor="#000000" align="center" height="25"  > 欠款金额 </td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 还款金额</td>
    <td   height="25" align="center" nowrap bordercolor="#000000"  bgcolor="#FFFFFF" > 未还款金额</td>
    </tr>
    <tr>
    <td nowrap  align="center" height="25" > <%=AA[m2][2]%></td> 
    <td nowrap  align="center" height="25" > <%=AA[m2][3]%></td>
    <td nowrap  align="center" height="25" > <%=AA[m2][8]%></td>
    <td  nowrap   height="25">¥<%=AA[m2][17]%> </td>  
    <td  nowrap   height="25">¥<%=huankuan1%></td>
    <td nowrap   height="25" >¥<%=weihuankuan%> </td> 
    </tr>
    <tr>
    <td colspan="12" height="10px" bgcolor="#D5D5D5"> </td></tr>

    <% }}

    %>

    <%}%>
    </table> </td></tr></table>
    <%
    float aa=(float)nRecordCount/20;
    float bb=(float)nRecordCount%20;
    if(bb==0){
    if(nPageNo>=aa){
    %>
    <br>
    <table  height="22" border="1"   align="center" cellpadding="0" cellspacing="0"bordercolor="#000000" style="width:740px;border:1px solid #000000;border-collapse:collapse ">
    <tr  height="18px">
    <td nowrap  align="center" width="20%" height="25"><b>本页欠款金额</b></td>

     <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum1%>',2));
                                    </script></td>

    <td nowrap  align="center" width="20%" height="25"><b>本页还款金额</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum2%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>本页未还款金额</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum3%>',2));
                                    </script></td> </tr>
    <tr  height="18px">
    <td nowrap  align="center" width="20%" height="25"><b>欠款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum4%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>还款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum5%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>未还款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum6%>',2));
                                    </script></td> </tr>
      </table><%}}else{
    if(nPageNo>aa){
    %>
    <br>
    <table  height="22" border="1"   align="center" cellpadding="0" cellspacing="0"bordercolor="#000000" style="width:740px;border:1px solid #000000;border-collapse:collapse ">
    <tr  height="18px">
    <td nowrap  align="center" width="20%" height="25"><b>本页欠款金额</b></td>

     <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum1%>',2));
                                    </script></td>

    <td nowrap  align="center" width="20%" height="25"><b>本页还款金额</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum2%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>本页未还款金额</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum3%>',2));
                                    </script></td> </tr>
    <tr  height="18px">
    <td nowrap  align="center" width="20%" height="25"><b>欠款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum4%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>还款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum5%>',2));
                                    </script></td>
    <td nowrap  align="center" width="20%" height="25"><b>未还款金额合计</b></td>

    <td nowrap  align="left" width="13%">¥<script>
                               document.write( FormatNumber('<%=sum6%>',2));
                                    </script></td> </tr>
      </table><%}}%>
    </form>
    <form id="form2" name="form2" method="post" action="">
    <%@ include file="../GuZi/fanye.jsp"%><br><table>
      <tr>
      <td  align=right  style="width:740px">
             <input type="button" style="width:60px" name="ButtonQuery" value=" 全部打印 " class="ButtonQuery" onClick="javascript:pass();;" ID="Button1">&nbsp;
     
      <input type="button" style="width:60px" name="ButtonQuery" value=" 打印本页 " class="ButtonQuery" onClick="return   printform();" ID="Button1">&nbsp;


    <input type="button" name="ButtonQuit" value="<%=codeToString(BUTTON_QUIT)%>" class="ButtonClass" onClick="javascript:window.location.href='Sales_CreditNotesReportList.jsp'"> 
    </td></tr> </table>
    </form>
    <script LANGUAGE="javascript">function pass2(){
    if("<%=biaoshi%>"=="1")
        {
     printform();
     window.close();
     }
    }
    pass2();
    </script>
    </body>
    </html>