Java开发学习笔记之Java导出JSP页面Table数据到EXCEL

最近在公司中的开发遇到一个功能:将SQL查到的数据导出到EXCEL中,自己再网上看了很多案例;在此记录一下自己完成这个功能的整个过程。


首先介绍一下我目前所做的功能:一个报表查询打印的功能;将数据查出并利用水晶报表进行打印,目前还在研究数据的导出,后续也会记录水晶报表的使用过程;


先贴出我的JSP页面吧:

<%@page import="org.apache.velocity.runtime.directive.Foreach"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <style>
        .point{position:absolute;left:50%; top:50%;}
        .pop{width:500px; height:500px; position:absolute;left:-250px; top:-250px; border:2px solid red; }
    </style>
<%@ include file="/resources/common/constant.jsp"%>


<title>交接处理</title>
<!--框架必需start-->
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<script type="text/javascript"
	src="${ctx }/resources/qui/libs/js/jquery.js"></script>
<script type="text/javascript"
	src="${ctx }/resources/qui/libs/js/framework.js"></script>
<script type="text/javascript" src="${ctx }/resources/qui/libs/js/jQuery_FixedTableHead.js"></script>

<link href="${ctx }/resources/qui/libs/css/import_basic.css"
	rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" id="skin"
	prePath="${ctx }/resources/qui/" />
<link rel="stylesheet" type="text/css" id="customSkin" />


	
<!--框架必需end-->
<!-- 日期选择框start -->
<script type="text/javascript"
	src="${ctx }/resources/qui/libs/js/form/datePicker/WdatePicker.js"></script>
<!-- 日期选择框end -->

<!--数字分页start-->
<script type="text/javascript"
	src="${ctx }/resources/qui/libs/js/nav/pageNumber.js"></script>
<!--数字分页end-->
<!--  <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 type="text/javascript">
  
  		$(document).ready(function(){
  			var $checkAllImg = $("#checkAll").find("img");
			$checkAllImg.click(function(){
				var checkField = $("#handshakingInfoTable").find("input[name=rId]");
				if(this.src.indexOf("checkAllOff.gif")<0)
					checkField.attr("checked","checked");
				else
					checkField.removeAttr("checked");
			});
			var column = $("#sort_column").val();
			var sort = $("#sort_order").val();
			if(column != "" && sort != ""){
				if(sort == "desc"){
					$("#sort_"+column).removeClass("sort_off").addClass("sort_down");
				}else{
					$("#sort_"+column).removeClass("sort_off").addClass("sort_up");
				}
			}
		
			$("#pageContent").bind("pageChange",function(e, index){
				var pageSize = $(this).attr("pageSize");
				//window.location.href = "${ctx }/base/driverInfo/list.do?pageSize="+pageSize+"&curPage="+(index+1)+"&"+getWhere();
			}).bind("sizeChange",function(e, num){
				//window.location.href = "${ctx }/base/driverInfo/list.do?pageSize="+num+"&curPage=1&"+getWhere();
			});
	 
		});
  		
  		
		
		// 获取查询条件 
		function getwhere(){
			/* var wavekeystart = encodeURI(encodeURI($("#WaveKeyStart").val()));
			var wavekeyend = encodeURI(encodeURI($("#WaveKeyEnd").val()));
			var status = encodeURI(encodeURI($("#stat").val()));
			return "&wavekeystart="+wavekeystart+"&wavekeyend="+wavekeyend+"&sta="+status;
			 */
		} 
		
		
		//查询
		function search() {
			/* var wavekeystart = document.getElementById("WaveKeyStart").value; 
			var wavekeyend = document.getElementById("WaveKeyEnd").value;
			var status = document.getElementById("stat").value;
			//alert("Wave开始:"+wavekeystart+"到:"+wavekeyend+"状态为:"+status);
			window.location.href = jsPath+"/samsung/printlist.do?&wavekeystart="+wavekeystart+"&wavekeyend="+wavekeyend+"&sta="+status;
			 */
		}
		
		//导出数据至EXCEL
   		function exportto() {
   		
		}
		
  </script>
</head>

<body>	
<!-- 交接处理 -->
	
	<form name="PrintFrom" id="PrintFrom">
		<div class="position">
			<div class="center">
				<div class="left">
					<div class="right">
						<span>当前位置:交接处理 >> 交接处理</span>
					</div>
				</div>
			</div>
		</div>
		<div style="background-color:#F0F0F0;"  id="box3">
			<table>
				<tr align="center">
					<td>
						<span>WAVE号:</span>
						<input type="text" id="WaveKeyStart" name="WaveKeyStart" value="${wavekeystart}" />
						<span>TO:</span>
						<input type="text" id="WaveKeyEnd" name="WaveKeyEnd" value="${wavekeyend}" />
					</td>
					<td>
						<span>      订单号:</span>
						<input type="text" id="OrderKeySatrt" name="OrderKeySatrt" value="${orderkeysatrt}" />
						<span>TO:</span>
						<input type="text" id="OrderKeyEnd" name="OrderKeyEnd" value="${orderkeyend}" />
					</td>
					<td><button class="hand" type="button" οnclick="search()"><span >查询</span></button></td>
				</tr>
			</table>
		</div>
			<div class="center">
				<div class="left">
					<div class="right">
						<div class="padding_top5 padding_left10">

							<button class="hand" type="button" οnclick="exportto()"><span >导出EXCEL</span></button>
							
							<button class="hand" type="button" οnclick="print()"><span >打印交接单</span></button>
							
							<button class="hand" type="button" οnclick=""><span >扫描出库</span></button>
						</div>
					</div>
				</div>
			</div>
			<div class="clear"></div>
		</div>
		<div id="scrollContent" class="margin_right5" >
			<table id="handshakinfInfotable" class="tableStyle" useClick="true" useCheckBox="true" sortMode="true" align="center" >
				<thead>
				<tr align="center">
					<th width="5%" >选择</th>
					<th width="20%">WAVE</th>
					<th width="25%">客户订单号</th>
					<th width="10%">打印状态</th>
					<th width="10%">订单数</th>
					<th width="10%">箱数</th>
					<th width="10%">已扫描</th>
					<th width="10%">未扫描</th>
				</tr>
				</thead>
				<tbody>
				<%-- <c:forEach var="p" items="${printList}" varStatus="status"> --%>
					<tr align="center">
						<td width="5%"><input class="groupclass" name="rId" type="checkbox" value="${p.wavekey}" autocomplete="off"/></td>
						<td width="20">111</td>
						<td width="25">222</td>
						<td width="10">333</td>
						<td width="10">444</td>
						<td width="10">555</td>
						<td width="10">666</td>
						<td width="10">777</td>
					</tr>
					<tr align="center">
						<td width="5%"><input class="groupclass" name="rId" type="checkbox" value="${p.wavekey}" autocomplete="off"/></td>
						<td width="20">111</td>
						<td width="25">222</td>
						<td width="10">333</td>
						<td width="10">444</td>
						<td width="10">555</td>
						<td width="10">666</td>
						<td width="10">777</td>
					</tr>
				<%-- </c:forEach> --%>
				</tbody>
				<%-- <c:if test="${fn:length(p) eq 0}">
					<tr>
						<td colspan="9" align="center"><label class="red">没有找到符合条件的记录!</label>
						</td>
					</tr>
				</c:if> --%>
			</table>
		</div>
		<div id="bottomPage"class="pagination_style">
			<div class="float_left padding5">${pageBean.statistics }</div>
			<div class="float_right padding5">
				<div id="pageContent" class="pageNumber"
					page=" ${pageBean.curPage-1 }" total="${pageBean.totalRow }"
					pageSize="${pageBean.pageSize }" showSelect="true" showInput="true"
					selectData='{"list":[{"key":10,"value":10},{"key":15,"value":15},{"key":20,"value":20},{"key":30,"value":30},{"key":50,"value":50}]}'></div>
			</div>
			<div class="clear"></div>
		</div>
		<input type="hidden" id="sort_column" value="${searchParam.column}" />
		<input type="hidden" id="sort_order" value="${searchParam.sort}" />
	</form>
</body>

</html>

数据应该是从后台传过来的,但是现在我想先测试一下,所以就自己随便写了几条数据测试是否能够将这几条数据导出到EXCEL中;


未完待续............



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值