web程序:将数据生成一个excel表格并下载

该博客介绍了如何在Web程序中使用jsp将数据动态生成为Excel表格,并提供下载。关键步骤包括设置响应头,引入Apache POI库,以及利用JSTL遍历数据填充表格。整个过程涉及到的内容类型设置、Content-Disposition头的使用以及表格样式定义等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只需要在jsp文件头上设置,其关键代码如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    response.setHeader("Pragma ", "public");
    response.setHeader("Cache-Control ", "must-revalidate, post-check=0, pre-check=0 ");
    response.addHeader("Cache-Control ", "public ");
    response.addHeader("Content-Disposition", "attachment; filename=user.xls");
    response.setContentType("application/vnd.ms-excel.numberformat:@;charset=UTF-8");
%>
<html>

注意添加相关依赖:

             <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.10.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.10.1</version>
            </dependency>


其整个jsp文件如下:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    response.setHeader("Pragma ", "public");
    response.setHeader("Cache-Control ", "must-revalidate, post-check=0, pre-check=0 ");
    response.addHeader("Cache-Control ", "public ");
    response.addHeader("Content-Disposition", "attachment; filename=user.xls");
    response.setContentType("application/vnd.ms-excel.numberformat:@;charset=UTF-8");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<style>
table {
    border: thin solid #333;
    border-collapse: collapse;
    border-spacing: 0px;
}

th {
    background-color: #ccc;
    border: thin solid #333;
}

td {
    border: thin solid #000;
}

</style>
</head>
<body>
    <table cellpadding="0" cellspacing="1" align="center"  class="global_tableresult" style="width: 100%">
            <tr>
                <th width="8%">姓名</th>
                <th width="8%">年龄</th>
                <th width="8%">工作</th>
                <th width="5%">地址</th>
                <th width="10%">电话</th>
                
            </tr>
            <c:forEach items="${ulist}" var="user">
                <tr>
                    <td>${user.name }</td>
                    <td>${user.age}</td>
                    <td>${user.job }</td>
                    <td>${user.addr}</td>
                    <td>${user.tel }</td>

                </tr>
            </c:forEach>
        </table>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值