【Java】Web打印

      项目中总是会用到Web页面打印的功能,下面小编就简单介绍一下自己的经验。

【smsx控件】

下载地址:http://download.csdn.net/detail/nxgliming/6668999

jsp页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../include.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	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>回执打印</title>
<object id="factory" style="display: none"
	classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
	codebase="print/smsx.cab"> </object>
<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">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/transport/printCarReceipt.js"></script>
<style type="text/css">
table.dataintable {
	margin-top: 10px;
	border-collapse: collapse;
	border: 1px solid #aaa;
	width: 100%;
}

table.dataintable td {
	vertical-align: baseline;
	padding: 5px 15px 5px 6px;
	border: 1px solid #aaa;
	text-align: left;
}
</style>
</head>
<body>
	<div id="NoPrint" class="noPrint"
		style="text-align: center; margin-top: 30px">
		<div>
			<a id="print" οnclick="print()" class="easyui-linkbutton c1"
				style="width: 120px">直接打印</a> <a id="printSet" οnclick="printSet()"
				class="easyui-linkbutton c1" style="width: 120px">打印设置</a> <a
				id="printView" οnclick="printPreivew()" class="easyui-linkbutton c1"
				style="width: 120px">打印预览</a> <a id="moveBackClick"
				οnclick="moveBackClick()" class="easyui-linkbutton c1"
				style="width: 120px">取消返回</a>
		</div>
	</div>
	<!-- 打印表格 -->
	<div id="report1page1" region="center"
		style="text-align: center; margin-top: 30px">
		<table border="1px" align="center" style="font: 18px 宋体;">
			<tr>
				<td colspan="4" style="text-align: center;height:35px;:valign:middle"><span style="font-size:20px;font-weight:bold;">到车回执</span></td>
			</tr>
			<tr>
				<td>承运单号</td>
				<td id="S_ID">${S_ID }</td>
				<td>运输商名称</td>
				<td id="S_TCompany">${S_TCompany }</td>
			</tr>
			<tr>
				<td>售达方名称</td>
				<td id="S_CCompany">${S_CCompany }</td>
				<td>送达方名称</td>
				<td id="S_RCompany">${S_RCompany }</td>
			</tr>
			<tr>
				<td>发运总量</td>
				<td id="sendCount">${receiveCount }</td>
				<td>车辆标识</td>
				<td id="uniqueNo">${uniqueNo }</td>
			</tr>
			<tr>
				<td>签收数量</td>
				<td id="receiveCount">${receiveCount }</td>
				<td>签收备注</td>
				<td id="S_TReturnRemark">无</td>
			</tr>
			<tr>
				<td>发车确认时间</td>
				<td id="S_CTime">${S_CTime }</td>
				<td>到车确认时间</td>
				<td id="S_TReturnTime">${S_TReturnTime }</td>
			</tr>
			<tr>
				<td>核定在途时间</td>
				<td id="S_InTrasitTime">${S_InTrasitTime }</td>
				<td>到车延误时间</td>
				<td id="receivedDelayTime">无延误</td>
			</tr>
			<tr>
				<td>接车人姓名</td>
				<td id="S_RCPerson">${S_RCPerson }</td>
				<td>接车人电话</td>
				<td id="S_RTel">${S_RTel }</td>
			</tr>
			<tr>
				<td colspan="4" style="text-align: center;height:35px;:valign:middle">受达方签章:</td>
			</tr>
		</table>
	</div>
</body>
</html>
js代码:

// 打印设置按钮
function printSet() {
	if (!finishText()) {
		return false;
	}
	factory.printing.PageSetup();
}

// 默认的打印设置
function SetPrintSettings() {

	factory.printing.header = "";
	factory.printing.footer = "";
	factory.printing.portrait = true;// 方向,true
	factory.printing.leftMargin = 5.0;// mm
	factory.printing.topMargin = 5.0;
	factory.printing.rightMargin = 5.0;
	factory.printing.bottomMargin = 3.0;

}

// 打印预览按钮
function printPreivew() {
	if (!finishText()) {
		return false;
	}
	document.getElementById("NoPrint").style.display = "none";
	factory.printing.Preview();
	document.getElementById("NoPrint").style.display = "block";

}

// 直接打印按钮
function print() {
	if (!finishText()) {
		return false;
	}
	document.getElementById("NoPrint").style.display = "none";
	factory.printing.Print(false); // 设置为false,直接打印
	document.getElementById("NoPrint").style.display = "block";
}

// 返回取消
function moveBackClick() {
	window.parent.$('#tabs').tabs('close', '回执打印');
}
实现效果:

【jatoolsPrinter插件】

        这个提供一个网址给大家,详尽,可以看看:http://print.jatools.com/cases.htm

【Webbrowser插件】

       跟第一种方式类似,也需要在jsp页面中通过<object>标签引入控件,在js中直接调用方法即可,下面就简写代码了

<body>    
<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"> </object>
</body>  
<script>    
WebBrowser.ExecWB(1,1) //打开     
WebBrowser.ExecWB(2,1) //关闭现在所有的IE窗口,并打开一个新窗口     
WebBrowser.ExecWB(4,1) //保存网页        
WebBrowser.ExecWB(6,1) //打印     
WebBrowser.ExecWB(7,1) //打印预览     
WebBrowser.ExecWB(8,1) //打印页面设置       
WebBrowser.ExecWB(10,1) //查看页面属性     
WebBrowser.ExecWB(15,1) //撤销     
WebBrowser.ExecWB(17,1) //全选     
WebBrowser.ExecWB(22,1) //刷新     
WebBrowser.ExecWB(45,1) //关闭窗体无提示  
</script> 
【小结】

        以上就是小编实践过的方式了,还有其他的类似JQuery方式等,可以研究一下。未完待续。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
以下是Java Web打印Excel并每页都带表头的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.HeaderFooter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @WebServlet("/printExcel") public class PrintExcelServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = request.getParameter("filePath"); String fileType = filePath.substring(filePath.lastIndexOf(".") + 1); InputStream is = new FileInputStream(filePath); Workbook wb = null; if (fileType.equals("xls")) { wb = new HSSFWorkbook(is); } else if (fileType.equals("xlsx")) { wb = new XSSFWorkbook(is); } Sheet sheet = wb.getSheetAt(0); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=printExcel.xlsx"); int lastRowNum = sheet.getLastRowNum(); int pageSize = 20; // 每页显示的行数 int totalPage = (lastRowNum + 1) % pageSize == 0 ? (lastRowNum + 1) / pageSize : (lastRowNum + 1) / pageSize + 1; // 设置表头样式 CellStyle headerStyle = wb.createCellStyle(); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置页脚 HeaderFooter footer = sheet.getFooter(); footer.setRight("Page " + HeaderFooter.page() + " of " + HeaderFooter.numPages()); for (int i = 0; i < totalPage; i++) { int startRow = i * pageSize; int endRow = (i + 1) * pageSize - 1; if (endRow > lastRowNum) { endRow = lastRowNum; } // 设置每页的表头 Row row = sheet.getRow(0); Row headerRow = sheet.createRow(startRow); headerRow.setHeight(row.getHeight()); List<Cell> headerCells = row.getCells(); for (int j = 0; j < headerCells.size(); j++) { Cell cell = headerRow.createCell(j); cell.setCellValue(headerCells.get(j).getStringCellValue()); cell.setCellStyle(headerStyle); } // 设置每页的数据 for (int j = startRow + 1; j <= endRow; j++) { row = sheet.getRow(j); Row dataRow = sheet.createRow(j + 1); dataRow.setHeight(row.getHeight()); List<Cell> dataCells = row.getCells(); for (int k = 0; k < dataCells.size(); k++) { Cell cell = dataRow.createCell(k); cell.setCellValue(dataCells.get(k).getStringCellValue()); } } } wb.write(response.getOutputStream()); wb.close(); is.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ``` 在上述代码中,我们首先获取Excel文件的输入流,并通过文件类型判断创建Workbook对象。然后,设置响应的Content-Type和Content-Disposition头,以便浏览器可以正确下载Excel文件。 接下来,我们根据每页显示的行数和总行数计算出总页数,并通过循环遍历每一页来设置表头和数据。在设置表头时,我们首先获取第一行的所有单元格,并创建一个新的行来设置表头样式和内容。在设置数据时,我们遍历每行的单元格,并将其内容复制到新创建的行中。 最后,我们通过Workbook对象的write方法将Excel文件的内容写入到响应的输出流中,然后关闭输入流和Workbook对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值