1. 程式人生 > >Hibernate-easyui 在IE瀏覽器中列印

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>