导出jsp为excel

    日常工作中经常会遇到想要把表单导出为excel的需求,可能大家首先想到的是poi,当然这种方式肯定是能事项的。但是除了这种方式,还有另一种个
    方式可以实现,废话不多说,直接上代码:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%
 String name="文件名称.xls";
 response.reset();
 response.setContentType("application/ms-excel;charset=GBK");
 response.setHeader("Content-Disposition", "attachment;filename="+new String(name.getBytes(), "ISO8859-1"));
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<table width="50%" border="1" align="center" cellpadding="0" cellspacing="0" >  
    <tr><td align="center" colspan="7" style="font-size: 25px;"><strong>xxx明细清单</strong></td></tr>
    <tr class="div3">
        <td align="bottom" colspan="7" style="font-size: 12px;height:30px">
            <div>
                <strong>申请人:</strong>${outdepot.stUseName }
                &nbsp;&nbsp;&nbsp;
                <strong style="padding-left:100px">部门:</strong>${outdepot.stUseDept }
                &nbsp;&nbsp;&nbsp;
                <strong style="padding-left:100px">申请时间:</strong><fmt:formatDate pattern="yyyy-MM-dd"  value="${outdepot.dtOutdepotDate }"/>
            </div>
        </td>
    </tr>
    <tr align="center" style="font-size: 14px;">
        <td width="10%" height="20px"><strong>序号</strong></td>
        <td width="25%"><strong>物品名称</strong></td>
        <td width="25%"><strong>规格型号</strong></td>
        <td width="10%"><strong>计量单位</strong></td>
        <td width="10%"><strong>单价(元)</strong></td>
        <td width="10%"><strong>领取数量</strong></td>
        <td width="10%"><strong>小计</strong></td>
    </tr>
     <c:forEach var="applyDetail" items="${detailList }" varStatus="status">
        <tr align="center" style="font-size: 12px;">
            <td width="10%" height="20px">${status.index + 1 }</td>
            <td width="25%" height="20px">${applyDetail.stName }</td>
            <td width="25%" height="20px">${applyDetail.stModel }</td>
            <td width="10%" height="20px">${applyDetail.stPcs }</td>
            <td width="10%" height="20px">${applyDetail.inPrice }</td>
            <td width="10%" height="20px">${applyDetail.inOutdepotNum }</td>
            <td width="10%" height="20px">${applyDetail.inTotal }</td>
        </tr>
     </c:forEach>
    <tr class="div3">
        <td align="right" colspan="7" style="font-size: 20px;">
            <div>合计:${outdepot.stOutdepoTTotal }</div>
        </td>
    </tr>
    <tr class="div3">
        <td align="right" colspan="7" style="font-size: 20px;">
            <div>签字:&nbsp;&nbsp;&nbsp;</div>
        </td>
    </tr>
</table>
</body>
</html>
    上面的代码是一个小demo,当然直接拷贝肯定是不能用的,可以很明显的看到代码中用到了el表达式。因此,导出为excel首先是访问后台,然后跳转到该页面。填充出所有数据后以流的方式下载到本地。
    最后讲讲该方法的原理:无论是request还是response都包含一个头部信息,(要查看头部信息可F12查看,这里不细说)。浏览器在接收到response时会首先解析头部信息,浏览器会通过头部信息里的属性来决定是解析还是以文件的形式下载等。这里就是利用了该特性。仔细观察会发现其实上面的demo跟普通的jsp页面没有任何区别,唯一不同的也是导出jsp为excel的关键代码就是上面那几行java代码:
<%
 String name="办公用品申请明细清单.xls";
 response.reset();
 response.setContentType("application/ms-excel;charset=GBK");
 response.setHeader("Content-Disposition", "attachment;filename="+new String(name.getBytes(), "ISO8859-1"));
%> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值