导出jsp为excel
来源:互联网 发布:海森梅尔几线牌子 知乎 编辑:程序博客网 时间:2024/06/05 07:48
日常工作中经常会遇到想要把表单导出为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 } <strong style="padding-left:100px">部门:</strong>${outdepot.stUseDept } <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>签字: </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
- 导出jsp为excel
- jsp里的table导出为excel
- 将jsp内容导出为Excel表
- jsp导出Excel的简单方式 ContentType为Excel格式
- 为什么JSP导出为excel,word后丢失了格式?
- 将jsp内容导出为Excel表简单实例
- jsp导出excel poi
- jsp导出Excel
- jsp导出Excel(续)
- jsp 导出 word excel
- JSP导出Excel文档
- jsp导出Excel
- jsp导出Excel
- jsp导出Excel
- JSP导出EXCEL
- jsp导出Excel
- jsp实现导出(Excel)
- jsp 导出 excel
- 利用buildConfigField从另一种角度解决BuildConfig.DEBUG的坑。
- activity生命周期
- bzoj 1030: [JSOI2007]文本生成器
- ORACLE in与exists语句的区别
- EL表达式的用法(二)--执行运算
- 导出jsp为excel
- springmvc框架详解
- 禁止Html5在手机上屏幕页面缩放
- PAT 1018锤子剪刀布
- tcpdump抓包工具各参数详解
- URLEncode与URLDecode
- Day30-Redis&Jedis
- SM4密码算法(附源码)
- 抽象方法声明位置