Hibernate-easyui 在IE浏览器中打印

1.首先就是引入一个print.js的文件。下面就是print.js的内容,不用做任何的修改,直接复制粘贴就行。

/**
 * 打印js
 */

/**打印页面设置*/
function printsetup(){
	WebBrowser.execwb(8,1);  
}

/**打印页面预览  */
function printpreview(){
	WebBrowser.execwb(7,1);
	window.onfocus = complete;
}

function printit(){
	if (confirm('确定打印吗?')) {  
		WebBrowser.execwb(6,6);
		window.onfocus = complete;
	}
}

function printInit(top,bottom,left,rigt){
	var wb = document.getElementById("WebBrowser");
	if (wb){
		document.getElementById("printBtn").onclick = printit;
		document.getElementById("printpreviewBtn").onclick = printpreview;
		setupPage(top,bottom,left,rigt);
	}else{
	}
}

function complete(){
	window.onfocus = null;
}

/**打印帮助*/
function printhelp(){
	window.open('/admin/help/printHelp.jsp'); 
}

var HKEY_Root,HKEY_Path,HKEY_Key;    
HKEY_Root="HKEY_CURRENT_USER";    
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";  

function setupPage(top,bottom,left,rigt) { 
   try{ 
	   if(!top){
		   top = "0.60";
	   }
	   if(!bottom){
		   bottom = "0.70";
	   }
	   if(!left){
		   left = "0.70";
	   }
	   if(!rigt){
		   rigt = "0.70";
	   }
	   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,"&b  &p/&P  &b") //去掉了&u 因为我不想显示当前打印页的网址 
	   HKEY_Key="margin_top"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,top); 
	   HKEY_Key="margin_bottom"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,bottom); //0.39相当于把页面设置里面的边距设置为10 
	   HKEY_Key="margin_left"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,left); 
	   HKEY_Key="margin_right"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,rigt); 
	   HKEY_Key="Print_Background"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"yes"); 
	   HKEY_Key="Shrink_To_Fit"; 
	   RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"yes"); 
	
   } catch(e){
	   alert(e)
   } 
} 

2.就是打印的按钮,我加了一个点击事件。

<button onclick="dyhmc()">打印花名册</button>

3.写打印按钮的点击事件。选中一行的数据,获得到这行数据的id,id作为参数,传到后台,查询出相关的内容。

function dyhmc() {
		var row = $('.easyui-datagrid').datagrid('getSelected');
		if (row) {
			window.location.href = '${ctx}/admin/xsgl/bjxx/dyhmc/' + row.id;
		} else {
			$.messager.alert('提示', '未选择任何数据', 'info')
		}
	}

4.在conreoller层中,封装一下,30个学生存到一个list中。

@RequestMapping("/dyhmc/{id}")
	public String dyhmc(@PathVariable("id") Long bjid, Model model) {
		List<Xsxx> xsxxList = xsxxService.findXsByBjId(bjid);//根据班级的id,查询出这个班级中的所有的学生
		Map<String, Object> map = new HashMap<>();
		//得到学生的数量,30个人占一页显示
		int count = xsxxList.size()/30;
		int yu = count%30;//取余
		if(yu>0) {//判断余数是否大于0
			count = count + 1;
		}
		for(int i = 0;i<count;i++) {
			List<Xsxx> xsxx = new ArrayList<>();
			List<Xsxx> xsxx2 = new ArrayList<>();
			if(i == count - 1) {//30个人放在一个集合中
				xsxx = xsxxList.subList(i*30, xsxxList.size());
				map.put("xsxx", xsxx);
			}else {
				xsxx2 = xsxxList.subList(i*30, 30*(i+1));
				map.put("xsxx2", xsxx2);
			}
		}
		//得到的数据向前台传过去
		model.addAttribute("map", map);
		model.addAttribute("count", count);
		model.addAttribute("xsxxList", xsxxList);
		return "/xsgl/bjxx/dyhmc";//跳转到页面
	}

5.在service中根据班级的id,查询这个班级存在的所有的学生。

public List<Xsxx> findXsByBjId(Long bjid) {
		Map<String, Object> map = new HashMap<>();
		final String hql = "from Xsxx where bj_id = :bjid";
		map.put("bjid", bjid);
		List<Xsxx> xsxx = this.query(hql, map);
		if (xsxx == null) {
			return null;
		} else {
			return xsxx;
		}

	}

6.最后搭建打印的页面。去遍历循环,后台传过来的值。点击打印即可。

<body onload="printInit(0.4, 0.4, 0.4, 0.4)">
	<div align="center" class="noprint" style="margin-top: 10px;">
		<OBJECT ID="WebBrowser" name="WebBrowser" width="0" height="0"
			CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></OBJECT>
		<input type="button" class="button" value="打印帮助" onclick="printhelp()" />
		<input type="button" class="button" value="打印设置"
			onclick="printsetup()" /> <input type="button" class="button"
			value="打印预览" onclick="printpreview()" id="printpreviewBtn" /> <input
			type="button" class="button" value="直接打印"
			onclick="alert('正在加载请稍后。。。')" id="printBtn" /> <br> <br>
	</div>
	<div class="bjmc"
		style="width: 595px; text-align: center; font-size: 22px; margin: 0 0 10px 0">${map.xsxx[0].bjgl.bjmc}</div>
	<div style="width: 595px; height: 800px;">
		<c:forEach items="${map.xsxx2}" var="xsxx">
			<div id="dyhmc"
				style="float: left; width: 95px; margin: 10px; height: 105px">
				<img src="${ctx}/xszp?xh=${xsxx.bh}&sfzh=${xsxx.sfzh}"
					style="width: 95px; height: 105px;">
				<div style="text-align: center; width: 90px;">${xsxx.xm}</div>
			</div>
		</c:forEach>
	</div>
	<div class="pageNext" style="width: 595px; height: 800px;page-break-after: always;">
			<c:forEach items="${map.xsxx}" var="xs">
				<div id="dyhmc"
					style="float: left; width: 95px; margin: 10px; height: 105px">
					<img src="${ctx}/xszp?xh=${xs.bh}&sfzh=${xs.sfzh}"
						style="width: 95px; height: 105px;">
					<div style="text-align: center; width: 90px;">${xs.xm}</div>
				</div>
			</c:forEach>
	</div>
</body>

猜你喜欢

转载自blog.csdn.net/cxc_happy111/article/details/85529426