通过js将table表格数据下载为Excel表格
1.创建用于下载数据的excel_create.jsp页面
<%@ page contentType="text/html; charset=utf-8" %>
<%
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+new String("download.xls"));
%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head><title>生成的EXCEL</title></head>
<body>
<table borderColor=#111111 cellSpacing=0 cellPadding=2 width=1200 align=center border=1>
<%=request.getParameter("tablevalue2")%>
</table>
</body>
</html>
2.创建提交数据表格的JS方法
//导出通用excel
function getXlsFromJsp(id)
{
var form_excel=document.createElement("form");
document.body.appendChild(form_excel);
form_excel.action="/excel_create.jsp?tableid="+id;
form_excel.method="post";
form_excel.setAttribute("accept-charset", "utf-8");
var table_inn = document.createElement("input");
table_inn.name="tablevalue2";
table_inn.type="hidden";
table_inn.value=document.getElementById(id).innerHTML;
form_excel.appendChild(table_inn);
form_excel.submit();
}
3.定义标签调用getXlsFromJsp方法
<input type="button" value="导出EXCEL" onClick="getXlsFromJsp('excel_table')" class="botton">
4.为table表格定义id属性
<table width="100%" border="0" cellspacing="1" cellpadding="4" class="newtable_0505" id="excel_table">
还没有评论,来说两句吧...