1、使用IE自带的WebBrowser控件进行打印,用户无需下载该控件,但缺点是需要设置IE浏览器的安全设置才能使用,“Internet选项”-->“安全”-->“自定义级别”-->“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”改为“启用”或者是“提示”,有些时候不同的系统、不同的浏览器版本可能设置的安全级别不一样,具体设置哪个立竿见影暂时没弄明白,刚才说的设置仅作参考我这么设置时好使过,可能换一台电脑也许又不会管用了 :cry: 。
2、区域打印
3、通过jQuery实现打印
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link href="/print.css" rel="stylesheet" />
<link href="/print.css" rel="stylesheet" media="print" />
<style type="text/css" media=print>
.noprint{display : none }
</style>
<script language="javascript">
// 打印页面设置
function printsetup() {
wb.execwb(8,1);
}
// 打印页面预览
function printpreview() {
wb.execwb(7,1);
}
function printit() {
if (confirm('确定打印吗?')) {
wb.execwb(6,6)
}
}
var HKEY_Root,HKEY_Path,HKEY_Key;
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
//设置网页打印的页眉页脚为空
function PageSetup_Null() {
try {
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
}catch(e){
}
}
//设置网页打印的页眉页脚为默认值
function PageSetup_Default() {
try {
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");
}catch(e){
}
}
</script>
</head>
<body>
<DIV align=center>
<OBJECT id="wb" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" name="wb"></OBJECT>
<input name="button_print" onclick="javascript:printit()" type="button" value="打印" class="noprint"/>
<input name="button_setup" onclick="javascript:printsetup();" type="button" value="打印页面设置" class="noprint"/>
<input name="button_show" onclick="javascript:printpreview();" type="button" value="打印预览" class="noprint" />
<input type="button" value="清空页码" onclick="PageSetup_Null()" class="noprint"/>
<input type="button" value="恢复页码" onclick="PageSetup_Default()" class="noprint"/>
</DIV>
这些文字就是要打印的内容!
</body>
</html>
2、区域打印
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<script language="javascript">
function preview() {
bdhtml = window.document.body.innerHTML;
sprnstr = "<!--startprint-->";
eprnstr = "<!--endprint-->";
prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
window.document.body.innerHTML = prnhtml;
window.print();
}
</script>
<body>
<form id="WebForm1" method="post">
<center>
本部分内容不被打印
</center>
<!--startprint-->
<div align="center">
<table>
<tr>
<td>
打印打印打印打印打印打印打印打印
</td>
</tr>
</table>
</div>
<!--endprint-->
<center>
本部分内容不被打印
</center>
<div align="center">
<input type="button" name="print" value="预览并打印" onclick="preview()">
</div>
</form>
</body>
</html>
3、通过jQuery实现打印
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<%@ page import="com.trht.session.SessionMng"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
SessionMng sessionmng = (SessionMng) request.getSession().getAttribute("sessionmng");
String ctxpath = request.getContextPath();
if("/".equals(ctxpath)){
ctxpath = "";
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language="JavaScript" type="text/javascript" src="<%=ctxpath%>/javascript/jquery/uploadify/jquery-1.4.2.min.js" ></script>
<script language="JavaScript" type="text/javascript" src="<%=ctxpath%>/javascript/jquery/uploadify/jquery.PrintArea.js" ></script>
<script>
$(document).ready(function(){
$("input#biuuu_button").click(function(){
$("div#myPrintArea").printArea();
});
});
</script>
</head>
<body>
<div id="noPrintArea">这部分内容不打印</div>
<div id="myPrintArea">只打印这部分内容</div>
<input id="biuuu_button" type="button" value="打印"></input>
</body>
</html>