jsp页面:
数据table:
<form action="/export.jsp" method="post" id="expform"> <input name="content" id="exportcontent" type="hidden"> </form> <table cellspacing="0" cellpadding="0" border="0" align="center" id="querybody" class="querybodyout"> <tbody> <tr id="queryhead"> <td width="6%" class="querybodyhead">船名</td> <td width="7%" class="querybodyhead">合同号</td> <td width="5%" class="querybodyhead">货物名称</td> <td width="7%" class="querybodyhead">车牌号</td> <td width="6%" class="querybodyhead">毛重(吨)</td> <td width="6%" class="querybodyhead">皮重(吨)</td> <td width="6%" class="querybodyhead">净重(吨)</td> <td width="6%" class="querybodyhead">具体收货地</td> <td width="6%" class="querybodyhead">货主</td> <td width="6%" class="querybodyhead">货代</td> <td width="8%" class="querybodyhead">皮重称量时间</td> <td width="8%" class="querybodyhead">毛重称量时间</td> </tr> <c:forEach items="${pager.items}" var="item"> <tr class="querybodytr" bgcolor="#F0F9FD"> <td>${item.sparestr1}</td> <td>${item.contractno}</td> <td>${item.sparestr4}</td> <td>${item.truckno}</td> <td>${item.gross/1000}</td> <td>${item.tare/1000}</td> <td>${item.net/1000}</td> <td>${item.sparestr8}</td> <td>${item.receiver}</td> <td>${item.sender}</td> <td> <fmt:formatDate value="${item.taredatetime}" pattern="yyyy-MM-dd"/> <br/> <fmt:formatDate value="${item.taredatetime}" pattern="HH:mm:ss"/> </td> <td> <fmt:formatDate value="${item.grossdatetime}" pattern="yyyy-MM-dd"/> <br/> <fmt:formatDate value="${item.grossdatetime}" pattern="HH:mm:ss"/> </td> </tr> <c:set value="${count + 1}" var="count" /> <%-- <c:set value="${sum + item.net}" var="sum" /> --%> </c:forEach> </tbody> </table>
点击导出执行的js方法:
function expExl(){ $("#exportcontent").val($(".querybodyout").prop("outerHTML").replace(/\ /g,'').replace("border=\"0\"","border=\"1\"")); $("#expform").submit(); }
表达提交的jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="java.net.URLEncoder"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String content = request.getParameter("content");
response.setContentType("APPLICATION/OCTET-STREAM; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" +new String(new String("疏港记录").getBytes("GBK"), "iso-8859-1") +".xls");
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=utf-8"> <style> <!-- @page {margin:.3in .6in .3in .6in; mso-header-margin:.3in; mso-footer-margin:.3in; mso-page-orientation:landscape; mso-horizontal-page-align:center;} .xl28 {font-size:9.0pt; vertical-align:middle;} --> </style> <xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>Sheet1</x:Name> <x:WorksheetOptions> <x:DefaultRowHeight>285</x:DefaultRowHeight> <x:FitToPage/> <x:Print> <x:PaperSizeIndex>9</x:PaperSizeIndex> <x:Scale>100</x:Scale> <x:HorizontalResolution>600</x:HorizontalResolution> <x:VerticalResolution>600</x:VerticalResolution> </x:Print> <x:CodeName>Sheet1</x:CodeName> <x:Selected/> <x:TopRowVisible>0</x:TopRowVisible> <x:ProtectContents>False</x:ProtectContents> <x:ProtectObjects>False</x:ProtectObjects> <x:ProtectScenarios>False</x:ProtectScenarios> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml> </head> <body> <%=content %>
<%--这里的content传递的是一个table--%> </body> </html>