关键代码:
<%@pagecontentType="application/msexcel"%>
<%
//response.setHeader("Content-disposition","inline;filename=videos.xls");
response.setHeader("Content-disposition","attachment;filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
简单测试例子:
<%@pagelanguage="java"import="java.util.*,java.io.*"pageEncoding="GBK"%>
<%@pagecontentType="application/msexcel"%>
<%
//response.setHeader("Content-disposition","inline;filename=videos.xls");
response.setHeader("Content-disposition","attachment;filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@pageimport="org.springframework.web.context.WebApplicationContext"%>
<%@pageimport="com.test.*"%>
<%@pageimport="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContextctx=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManagerum=(UserManager)ctx.getBean("userManager");
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>springjdbctest</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<tableborder="1"width="100%">
<tr><td>id</td><td>name</td>
</tr>
<%
List<User>users2=um.getUserList();
for(inti=0;i<users2.size();i++)
{
intt_id2=users2.get(i).getId();
Stringt_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2%></td><td><%=t_name2%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
|