网络编程 
首页 > 网络编程 > 浏览文章

JSP 导出Excel表格的实例

(编辑:jimmy 日期: 2025/5/1 浏览:3 次 )

java 后台返回一个ModelAndView 对象,然后加入这2行设置

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode("会员列表.xls", "UTF-8"));

也可以把这二行设置放入JSP中

在jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
 response.setContentType("application/vnd.ms-excel");
 response.setHeader("Content-disposition","attachment;filename=" + java.net.URLEncoder.encode("会员列表.xls", "UTF-8"));
%>
<!DOCTYPE html>
<html>
<head>
 <title>CMS-会员管理</title>
 <meta charset="utf-8" />
 <meta http-equiv="pragma" content="no-cache"/>
 <meta http-equiv="cache-control" content="no-cache"/>
 <meta http-equiv="expires" content="0"/>
</head>
<body>

<table id="sample-table-1" border="1" cellpadding="2" cellspacing="1">
 <thead>
 <tr>
  <th nowrap width="15%">序号</th>
  <th nowrap width="5%">卡号</th>
  <th nowrap width="5%">姓名</th>
  <th nowrap width="5%">年龄</th>

 </tr>
 </thead>
 <tbody>
 <c:forEach items="${list}" var="model" varStatus="status" >
  <tr>
   <td>${model.id}</td>
   <td>${model.cardNo}</td>
   <td>${model.name}</td>
   <td>${model.age}</td>
  </tr>
 </c:forEach>
 </tbody>
</table>
</body>
</html>

以上这篇JSP 导出Excel表格的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:JSP 获取用户的真实IP两种实现方法详解
下一篇:Jsp自定义标签和方法详解
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap