打印功能
效果图
需要打印的界面
<%@ page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false"%><%@ taglib prefix="s" uri="/struts-tags"%><%@ include file="/commons/taglib.jsp"%><head><%@ include file="/commons/header.jsp"%>
<object id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> </object>
一、引用代码:也就是在页面head内,加入一个object对象元素,元素的classid属性值是固定的,一个很长的唯一识别字符串,而id属性值是可以自定义的,大家习惯地把这个值设为大写字母LODOP,后面在使用控件功能时用它来代表控件。<script src="js/LodopFuncs.js" type="text/javascript"></script> <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width="0" height="0"> <embed id="LODOP_EM" type="application/x-print-lodop" width="0" height="0" pluginspage="install_lodop32.exe"></embed> </object>二、 做个链接:<a href="javascript:myPreview()"><b>打印预览</b></a>三、JS代码<script language="javascript" type="text/javascript"> var LODOP; //声明为全局变量 function myPreview() { CreatePrintPage(); LODOP.PREVIEW(); //打印预览 }; function CreatePrintPage() { LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); var strBodyStyle = "<style>" + document.getElementById("yangshi").innerHTML + "</style>"; //这里的“yangshi"是样式表的ID,如 <style type="text/css" id="yangshi"></style> var strFormHtml = strBodyStyle + "<body>" + document.getElementById("divdiv1").innerHTML + "</body>";//这里的”divdiv1“是标签的名称。 LODOP.PRINT_INIT("高青公路养护"); LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4") ; //A4纸张纵向打印 LODOP.SET_PRINT_STYLE("FontSize", 9); LODOP.ADD_PRINT_HTM("0%", "0%", "100%", "100%", strFormHtml);//四个数值分别表示Top,Left,Width,Height };</script>注意上面同一种颜色的字符要统一,否则后面就无法调用前面声明的变量等。
<script language="javascript">function showH(){var strr=$("#checked").val();var checkbox=document.getElementsByName("aids");var arr=strr.split(",");for(var i=0;i<arr.length;i++){for(var j=0;j<checkbox.length;j++){if(arr[i].trim()==checkbox[j].value){checkbox[j].checked=true;}}}}function Printpart(id_str)//id-str 内容中的id { var el = document.getElementById(id_str); var iframe = document.createElement('IFRAME'); var doc = null; iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;'); document.body.appendChild(iframe); doc = iframe.contentWindow.document; doc.write('<div>' + el.innerHTML + '</div>'); doc.close(); iframe.contentWindow.focus(); iframe.contentWindow.print(); if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe); } return false; }</script><link rel="Stylesheet" href="<c:url value='/css/nav.css" type="text/css'/>" /></head><body onload="showH()"><%@ include file="/bd/xlgzgl/xljkjyprint.jsp"%><s:form id="editForm" action="xljkjyAction_search" enctype="multipart/form-data" namespace='/bd'><s:hidden name="search" value="1"/><s:hidden name="xljkjy.aid"/><s:hidden name="xljkjy.cjsj"/><s:hidden name="xljkjy.cjzid"/><s:hidden name="xljkjy.cjzssbmid"/><div width="16" height="13" hspace="6" align="absmiddle" />心理健康教育 <s:if test="modify&&(xljkjy.aid!=null)"><div method="save" onclick="return confirm('确认提交?')" cssid="save" method="save" onclick="return confirmSave()" cssmethod="exportYffzjyExcel" cssvalue="打印" cssonclick="return Printpart('form1')"/></div></s:if><div css/></div><DIV class=topic_nav_block_wrapper><UL class=topic_nav_block id="myul1"><LI><A href="#">心理健康教育</A></LI></UL><DIV class=clear></DIV></DIV><div id="mainTables1"><div style="display:block;margin-left: 0px"><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#d9d6d6" width="15%">活动名称</td><td width="35%" ><s:textfield name="xljkjy.hdmc" id="hdmc"/></td> <td width="15%">活动时间</td> <td width="35%"><s:textfield name="xljkjy.hdsj" onclick="SelectDate(this,'yyyy-MM-dd')" readonly="true" size="20" id="hdsj"> <s:param name="value"><s:date name="xljkjy.hdsj" format="yyyy-MM-dd"/></s:param> </s:textfield></td></tr><tr> <td width="15%">活动地点</td> <td width="35%"><s:textfield name="xljkjy.hddd" id="hddd"/></td> <td width="15%">组织者</td> <td width="35%"><s:textfield name="xljkjy.zzr" id="zzr"/></td> </tr> <tr> <td width="15%">授课人</td> <td width="35%"><s:textfield name="xljkjy.skr" id="skr"/></td> <td width="15%">参加人员及数量</td> <td width="35%"><s:textfield name="xljkjy.cjrsl" id="cjrsl"/></td></tr><tr><%-- <td width="15%">参加人</td> <td width="35%" ><s:textarea name="xljkjy.cjr" cssStyle="width:99%" id="cjr"/></td> --%><td width="15%">活动形式</td><td width="35%" colspan="3"><s:if test="hdxs==null"><s:textfield name="xljkjy.hdxs" id="hdxs" readonly="true"/></s:if><s:else><s:textfield name="xljkjy.hdxs" id="hdxs" value="%{hdxs}" readonly="true"/></s:else></td> </tr> <tr> <td width="15%" style="height:50px;">活动内容</td> <td colspan="3" style="height:60px;"><s:textarea name="xljkjy.hdnr" cssStyle="height:100%;width:100%" id="hdnr"/></td></tr></table></div><%@ include file="/commons/uploadfile.jsp"%><s:actionmessage/><s:actionerror/></div></div></s:form></body>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <script type="text/javascript" src="../../js/jquery.js"></script><script type="text/javascript">$(document).ready(function(){var hkey_key; var hkey_root="HKEY_CURRENT_USER"; var hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\";var RegWsh = new ActiveXObject("WScript.Shell"); hkey_key="header"; RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,""); hkey_key="footer"; RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");})</script> </head> <body> <div style="display:none"> <form id="form1"> <div align="center" style="font-size: 12px"><h1>心理活动登记表</h1></div><p>单位: <s:property value="#session.loginUser.sysUsers.jgqc"/> </p><table width="100%" height="816" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse;"> <tr> <td height="41" width="25%"><div align="center">活动名称</div></td> <td height="41" width="75%"> <s:property value="xljkjy.hdmc"/> </td> </tr> <tr> <td height="41" width="25%"><div align="center">活动时间</div></td> <td height="41" width="75%"> <s:property value="xljkjy.hdsj"/></td> </tr> <tr> <td height="41" width="25%"><div align="center">活动地点</div></td> <td height="41" width="75%"> <s:property value="xljkjy.hddd" /></td> </tr> <tr> <td height="41" width="25%"><div align="center">组织者</div></td> <td height="41" width="75%"> <s:property value="xljkjy.zzr"/> </td> </tr> <tr> <td height="41" width="25%"><div align="center">参加人员及数量</td> <td height="41" width="75%"> <s:property value="xljkjy.cjrsl"/></td> </tr> <tr> <td height="164" width="25%"><div align="center">活动内容</div></td> <td height="164" width="75%"> <s:property value="xljkjy.hdnr" /></td> </tr> <tr> <td height="41" width="25%"><div align="center">活动形式</div></td> <td height="41" width="75%"> <s:property value="xljkjy.hdxs" /></td> </tr> <tr> <td height="41" width="25%"><div align="center">上传附件</div></td> <td height="41" width="75%"> <s:iterator value="filelist" status="st"> <s:property value="wjmc" /><br> </s:iterator></td> </tr></table></form> </div> </body></html>