杰表技术论坛 's Archiver

admin 发表于 2010-1-6 11:47

checkbox 选择性批量打印(转)

[url]http://blog.163.com/aili10000@126/blog/static/12002860120097251041124662/[/url] 原文出处

1、得到需要打印的checkbox列表,然后按需要选择 showbeanprint.jsp


<%@ page language="java" pageEncoding="UTF-8"%>

<%
request.setCharacterEncoding("utf-8");
%>
<%
String base = request.getContextPath();
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>



  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <!--这里的js是控制checkbox全选/取消-->
  <script language="javascript">
      var flag=1;
      function selectAll(){
      if(flag==1){
      if(form1.chxSong.length!=undefined){
      for (var i=0;i<form1.chxSong.length;i++)
      form1.chxSong[i].checked=true;
      }else{
      form1.chxSong.checked=true;
      }
      flag=0;
      }else{
      if(form1.chxSong.length!=undefined){
      for (var i=0;i<form1.chxSong.length;i++)
      form1.chxSong[i].checked=false;
      }else{
      form1.chxSong.checked=false;
      }
      flag=1;
      }
      }
</script>



  <style type="text/css">
<!--
body {
margin-left: 3px;
margin-top: 0px;
margin-right: 3px;
margin-bottom: 0px;
}
.STYLE1 {
color: #e1e2e3;
font-size: 12px;
}
.STYLE6 {color: #000000; font-size: 12; }
.STYLE10 {color: #000000; font-size: 12px; }
.STYLE19 {
color: #344b50;
font-size: 12px;
}
.STYLE21 {
font-size: 12px;
color: #3b6375;
}
.STYLE22 {
font-size: 12px;
color: #295568;
}

.pnav{
font-size: 12px;clear:left; margin: 10px 7px 0px 0px;
}
.pages-nav { height:24px;margin: 0; padding: 0px;}
.pages-nav .brackets, .pages-nav .mini-page{
display:none;
visibility:hidden;
}
.pages-nav .first-page{
margin-top:18px!important;
margin-top:19px;
}
.pages-nav a.backToTop:link{display:none;border:none;color:white;background:#FFF;}
.pages-nav a.backToTop:hover{color:white;background:white;}
.pages-nav a.backToTop:visited{color:#FFF;border:none;}
.pages-nav a, .pages-nav span { display: block; float: left; padding: 1px 5px 0px; margin-left: 4px; }
.pages-nav a, .pages-nav a:link, .pages-nav a:visited { background: #fff; border: 1px solid #ddd; color: #000; text-decoration: none; }
.pages-nav a:hover { background: #F30; border-color: #ddd; color: #fff; text-decoration: none; }
.pages-nav span { color: #999;}
.pages-nav span.next { color: #CCC;}
.pages-nav span.next1 { color: #CCC;  cursor:pointer; border:1px solid #DDDDDD;}
.pages-nav .current { border: 1px solid #fff; font-weight: bold; color: #000; }
.pages-nav .next { background: #fff; color: #000; border: 1px solid #ddd; }


-->
</style>
   
</head>

<body>
  <form name="form1" method="post" action="printtest">
   <table width="100%" border="0" align="center" cellpadding="0"
    cellspacing="0">
    <tr>
     <td height="30">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td height="24" bgcolor="#353c44">
         <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
           <td>
            <table width="100%" border="0" cellspacing="0"
             cellpadding="0">
             <tr>
              <td width="6%" height="19" valign="bottom">
               <div align="center">
                <img src="images/tb.gif" width="14" height="14" />
               </div>
              </td>
              <td width="94%" valign="bottom">
               <span class="STYLE1">基本信息列表</span>
              </td>
             </tr>
            </table>
           </td>
           <td>
            <div align="right">
             <span class="STYLE1"> <input type="checkbox"
               name="checkbox11" id="checkbox11" /> 全选 &nbsp;&nbsp;<img
               src="images/add.gif" width="10" height="10" />添加  &nbsp;
              <img src="images/del.gif" width="10" height="10" /> 编辑
              &nbsp;&nbsp;<img src="images/edit.gif" width="10"
               height="10" /> 删除 &nbsp;</span><span class="STYLE1">
              &nbsp;</span>
            </div>
           </td>
          </tr>
         </table>
        </td>
       </tr>
      </table>
     </td>
    </tr>


    <tr>
     <td>
      <table width="100%" border="0" cellpadding="0" cellspacing="1"
       bgcolor="#a8c7ce">
       <tr>
        <td width="8%" height="20" bgcolor="d3eaef" class="STYLE10">
         <div align="center">
          <input name="submit" type="submit" id="submit" value="打印" />

          <input type="checkbox" name="selall" onClick="selectAll();" />
         </div>
        </td>
        <td width="16%" height="20" bgcolor="d3eaef" class="STYLE6">
         <div align="center">
          <span class="STYLE10">事件类别:</span>
         </div>
        </td>
        <td width="40%" height="20" bgcolor="d3eaef" class="STYLE6">
         <div align="center">
          <span class="STYLE10">标题</span>
         </div>
        </td>
        <td width="20%" height="20" bgcolor="d3eaef" class="STYLE6">
         <div align="center">
          <span class="STYLE10">时间</span>
         </div>
        </td>
        <td width="5%" height="20" bgcolor="d3eaef" class="STYLE6">
         <div align="center">
          <span class="STYLE10">管理员</span>
         </div>
        </td>
        <td width="15%" height="20" bgcolor="d3eaef" class="STYLE6">
         <div align="center">
          <span class="STYLE10">操作</span>
         </div>
        </td>

       </tr>
       <c:set var="results" value="${requestScope.pager.list}"></c:set>
       <c:forEach items="${results}" var="result">


        <tr>
         <td height="20" bgcolor="#FFFFFF">
          <div align="center">
           <input type="checkbox" name="chxSong" value="${result.id }">


          </div>
         </td>
         <td height="20" bgcolor="#FFFFFF" class="STYLE6">
          <div align="center">
           <c:out value="${result.onetype}"></c:out>
          </div>
         </td>


         <td height="20" bgcolor="#FFFFFF" class="STYLE19">
          <div align="center">
           <a
            href="<%=base%>/servlet/editservlet?id=${result.id}&s=edit">
            ${result.title} </a>
          </div>
         </td>
         <td height="20" bgcolor="#FFFFFF" class="STYLE19">
          <div align="center">
           <c:out value="${result.datetime}" />
          </div>
         </td>
         <td height="20" bgcolor="#FFFFFF" class="STYLE19">
          <div align="center">
           <c:out value="${result.users}"></c:out>
          </div>
         </td>
         <td height="20" bgcolor="#FFFFFF" class="STYLE19">
          <div align="center">
           <a href=<%=base%>
            /servlet/editservlet?id=${result.id}&s=edit> 编辑 |</a><a
            href=<%=base%> /del.jsp?id=${result.id }>| 删除</a>
          </div>
         </td>

        </tr>
     </c:forEach>
    </table>
     </td>
    </tr>
   <tr>
     <td height="30">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td width="33%">
         <div align="left">
          <span class="STYLE22">&nbsp;&nbsp;&nbsp;&nbsp;共有<strong>${counts.maxElements
            } </strong> 条记录,共 <strong>${counts.maxPages }</strong> 页</span>
         </div>
        </td>
        <td width="67%">
        
        
         <table width="312" border="0" align="right" cellpadding="0"
          cellspacing="0">
          <tr>
           <div class="pnav">
<div class="pages-nav" style="margin: 10px 7px 0px 0px;padding:0 0 20px 0px!important;padding-bottom:0;">
        
                                            <!--pager标签开始url为传参访问的servlet页面,items为总记录数 maxPageItems为每页显示的条数,initParam.pagesize为从web.xml中读取pagesize,
                                                    根据这两项pager计算出总页数,maxIndexPages为显示多少个页符即12345,pageUrl,pageNumber名称不可改,pager会根据标签自动计算上页,下页
                                              pg:pages 为中间显示的数字及链接  -->
           <pg:pager url="togetservlet" items="${counts.maxElements}"
            maxPageItems="${initParam.pagesize}" maxIndexPages="10">
            <pg:index>
            <pg:first unless="current">
              <a href="${pageUrl}&p=${pageNumber}">首页</a>
             </pg:first>
            
             <pg:prev>
              <a href="${pageUrl}&p=${pageNumber}">上一页</a>
             </pg:prev>
             <pg:pages>
              <a href="${pageUrl}&p=${pageNumber}">${pageNumber}</a>
             </pg:pages>
             <pg:next>
              <a href="${pageUrl}&p=${pageNumber}">下一页</a>
             </pg:next>
             <pg:last unless="current">
              <a href="${pageUrl}&p=${pageNumber}">尾页</a>
             </pg:last>
            </pg:index>
           </pg:pager>

</div></div>



          </tr>
         </table>
         
        </td>
       </tr>
      </table>
     </td>
    </tr>





   </table>


  </form>

</body>
</html>



2、打印控制java,从页面取得checkbox 所列 id 组合成sql语句查询出相应结果 printtest.java

package com.chinaba.print;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;


public class printtest extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doPost(request, response);
}

/**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  request.setCharacterEncoding("utf-8");
  String[] chxSong = request.getParameterValues("chxSong");
  List list;
  Context ctx;
  try {
   ctx = new InitialContext();
   DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/sqlserver");
   QueryRunner qr = new QueryRunner(ds);
   String idss="";
   String par="";
         String para="";
        
         for(int i=0;i<chxSong.length;i++){
          String ids = chxSong[i];
         
          if (i==chxSong.length-1){
          para = "id="+ids;
            
             }else {
              par ="id="+ids+" or ";
    }
          idss=par+para;
         
         
              }
         System.out.print(idss);
         String sql = "select * from shenlei.dbo.word where "+idss+"";
         System.out.println(sql);
         list = (List)qr.query(sql,null,new MapListHandler());
         System.out.println(list);
         for(int j=0;j<list.size();j++){
          Map map =(Map)list.get(j);
          System.out.println(map.get("title"));
          }
   
  } catch (NamingException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  
   
  
}

}

3、利用jatoolsPrinter.cab 打印控件的div进行批量套打,一个div即为一页的打印内容 测试页

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>打印报检单</title>
<OBJECT ID="jatoolsPrinter" CLASSID="CLSID:B43D3361-D075-4BE2-87FE-057188254255" codebase="jatoolsPrinter.cab#version=2,1,0,3"></OBJECT>
<style type="text/css">
.STYLET{font-size: 13px}
.boderTD{font-size: 12px}
.boderTable{font-size: 12px}



.A4{
  height: 10mm;
  width: 216mm;
  /*border:#000 solid 1px;*/
  font-size:12px;
}
.report_name{
  font-size:18px;
  font-family: "楷体_GB2312";
  text-decoration: underline;
  font-weight:bold;
}
.company{
  font-size:24px;
}
.content{
  width:87%;
}
.left_top{
  border:#000 1px solid;font-family: 黑体;font-size: 12px;
}
.top{
  border-top:#000 1px groove;
  border-bottom:#000 1px groove;
  border-right:#000 1px groove;font-family: 黑体;font-size: 12px;
}
.left{
  border-bottom:#000 1px groove;
  border-right:#000 1px groove;
  border-left:#000 1px groove;font-family: 黑体;font-size: 12px;
}
.bottom{
  border-bottom:#000 1px groove;
  border-right:#000 1px groove;font-family: 黑体;font-size: 12px;
}
.c1{font-family: 黑体;font-size: 12px;}
</style>
<script>
function doPrint(how)
{
   
//打印文档对象
    var myDoc ={
        
     settings:{
        paperWidth : 2100,              
         paperHeight : 2970,            
         orientation : 1
      
     },
                 settingsID : "cardate",
     documents: document,    // 打印页面(div)们在本文档中
        copyrights  :   '杰创软件拥有版权  www.jatools.com'         // 版权声明必须
         };
        
    // 调用打印方法
    if(how == '打印预览...')
     jatoolsPrinter.printPreview(myDoc);   // 打印预览
               
    else if(how == '打印...')
     jatoolsPrinter.print(myDoc ,true);   // 打印前弹出打印设置对话框
               
    else
     jatoolsPrinter.print(myDoc ,false);       // 不弹出对话框打印
}
</script>
</head>


<body>
<div id='page1'>
<table width="95%" align="center" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td height="30" align="center" class="text1"><font size="5"><br><br>
          中华人民共和国XX出入境检验检疫<br>
              X市出入境货物报检单</font><br>
              <br><br><br></td>
        </tr>
</table>

<table width="95%" align="center"  cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="570" height="20" align="left" valign="middle"><span class="STYLET"><b>报检信息</b></span></td>
    <td width="83" align="right" valign="middle"><span class="STYLET">报检号:</span></td>
    <td width="23%" align="left" valign="middle"><span class="STYLET">&nbsp;B530520109000020</span></td>
  </tr>
</table>
<table width="95%" align="center" cellpadding="0" cellspacing="0" class="boderTable" border="1">
  <!--DWLayoutTable-->
  <tr>
    <td height="20" width="104" align="center" valign="middle" class="boderTD">出入境类别</td>
    <td width="181" align="center" valign="middle" class="boderTD">入境</td>
    <td width="100" align="center" valign="middle" class="boderTD">报检币种</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">人民币</td>
    <td width="98" align="center" valign="middle" class="boderTD">报检时间</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">2009-04-14 10:39</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">报检人代码</td>
    <td align="center" valign="middle" class="boderTD">B5305700006</td>
    <td align="center" valign="middle" class="boderTD">报检人名称</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">金峡</td>
    <td align="center" valign="middle" class="boderTD">报检人国籍</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">越南</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">证件类别</td>
    <td align="center" valign="middle" class="boderTD">工作证</td>
    <td align="center" valign="middle" class="boderTD">证件号码</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">6548754</td>
    <td align="center" valign="middle" class="boderTD">联系电话</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">15102314789</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">来源国(地区)</td>
    <td align="center" valign="middle" class="boderTD">越南</td>
    <td align="center" valign="middle" class="boderTD">来源地备注</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
    <td align="center" valign="middle" class="boderTD">运输方式</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">其他(指人扛、牲畜驮运等)</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">目的国(地区)</td>
    <td align="center" valign="middle" class="boderTD">中国</td>
    <td align="center" valign="middle" class="boderTD">目的国备注</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
    <td align="center" valign="middle" class="boderTD">运输工具号</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
  </tr>
  <tr>
    <td height="26" align="center" valign="middle" class="boderTD">序号</td>
    <td align="center" valign="middle" class="boderTD">货物名称</td>
    <td align="center" valign="middle" class="boderTD">HS编码</td>
    <td width="81" align="center" valign="middle" class="boderTD">CIQ编码</td>
    <td width="89" align="center" valign="middle" class="boderTD">原产国(地区)</td>
    <td align="center" valign="middle" class="boderTD">数(重)量</td>
    <td width="87" align="center" valign="middle" class="boderTD">计量单位</td>
    <td width="97" align="center" valign="middle" class="boderTD">原始货值</td>
  </tr>

  <tr align="center" valign="middle" class="boderTD">
    <td height="20" align="center" valign="middle" class="boderTD">1</td>
    <td align="center" valign="middle" class="boderTD">荔枝</td>
    <td align="center" valign="middle" class="boderTD">0810901000</td>
<td align="center" valign="middle" class="boderTD">04110799</td>
<td align="center" valign="middle" class="boderTD">越南</td>
    <td align="center" valign="middle" class="boderTD">3000.0</td>
    <td align="center" valign="middle" class="boderTD">千克</td>
<td align="center" valign="middle" class="boderTD">90000</td>
  </tr>

</table>
</div>


<div id='page2'>
<table width="95%" align="center" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td height="30" align="center" class="text1"><font size="5"><br><br>
          中华人民共和国XX出入境检验检疫<br>
              X市出入境货物报检单</font><br>
              <br><br><br></td>
        </tr>
</table>

<table width="95%" align="center"  cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="570" height="20" align="left" valign="middle"><span class="STYLET"><b>报检信息</b></span></td>
    <td width="83" align="right" valign="middle"><span class="STYLET">报检号:</span></td>
    <td width="23%" align="left" valign="middle"><span class="STYLET">&nbsp;B530520109000020</span></td>
  </tr>
</table>
<table width="95%" align="center" cellpadding="0" cellspacing="0" class="boderTable" border="1">
  <!--DWLayoutTable-->
  <tr>
    <td height="20" width="104" align="center" valign="middle" class="boderTD">出入境类别</td>
    <td width="181" align="center" valign="middle" class="boderTD">入境</td>
    <td width="100" align="center" valign="middle" class="boderTD">报检币种</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">人民币</td>
    <td width="98" align="center" valign="middle" class="boderTD">报检时间</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">2009-04-14 10:39</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">报检人代码</td>
    <td align="center" valign="middle" class="boderTD">B5305700006</td>
    <td align="center" valign="middle" class="boderTD">报检人名称</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">金峡</td>
    <td align="center" valign="middle" class="boderTD">报检人国籍</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">越南</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">证件类别</td>
    <td align="center" valign="middle" class="boderTD">工作证</td>
    <td align="center" valign="middle" class="boderTD">证件号码</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">6548754</td>
    <td align="center" valign="middle" class="boderTD">联系电话</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">15102314789</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">来源国(地区)</td>
    <td align="center" valign="middle" class="boderTD">越南</td>
    <td align="center" valign="middle" class="boderTD">来源地备注</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
    <td align="center" valign="middle" class="boderTD">运输方式</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">其他(指人扛、牲畜驮运等)</td>
  </tr>
  <tr>
    <td height="20" align="center" valign="middle" class="boderTD">目的国(地区)</td>
    <td align="center" valign="middle" class="boderTD">中国</td>
    <td align="center" valign="middle" class="boderTD">目的国备注</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
    <td align="center" valign="middle" class="boderTD">运输工具号</td>
    <td colspan="2" align="center" valign="middle" class="boderTD">&nbsp;</td>
  </tr>
  <tr>
    <td height="26" align="center" valign="middle" class="boderTD">序号</td>
    <td align="center" valign="middle" class="boderTD">货物名称</td>
    <td align="center" valign="middle" class="boderTD">HS编码</td>
    <td width="81" align="center" valign="middle" class="boderTD">CIQ编码</td>
    <td width="89" align="center" valign="middle" class="boderTD">原产国(地区)</td>
    <td align="center" valign="middle" class="boderTD">数(重)量</td>
    <td width="87" align="center" valign="middle" class="boderTD">计量单位</td>
    <td width="97" align="center" valign="middle" class="boderTD">原始货值</td>
  </tr>

  <tr align="center" valign="middle" class="boderTD">
    <td height="20" align="center" valign="middle" class="boderTD">1</td>
    <td align="center" valign="middle" class="boderTD">荔枝</td>
    <td align="center" valign="middle" class="boderTD">0810901000</td>
<td align="center" valign="middle" class="boderTD">04110799</td>
<td align="center" valign="middle" class="boderTD">越南</td>
    <td align="center" valign="middle" class="boderTD">3000.0</td>
    <td align="center" valign="middle" class="boderTD">千克</td>
<td align="center" valign="middle" class="boderTD">90000</td>
  </tr>

</table>
</div>



<table border="0" cellpadding="0" cellspacing="0" width="100%" id="AutoNumber1" height="45">
  <tr>
    <td width="33%" height="45" valign="top"><br>
      &nbsp;&nbsp;&nbsp;
        <input type="button" value="打印预览" onClick="doPrint('打印预览...')">
        <input name="button" type = "button" onClick = "doPrint('打印...')" value = "打印...">
         <input type="button" value="打印" onClick="doPrint('打印')">&nbsp;</td>
    <td width="67%" height="45" valign="top" class="normal">&nbsp;</td>
  </tr>
</table>
</body>

</html>

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.