在java web项目中经常会进行excel的导入导出,今天介绍一种非常简洁不需要额外的jar的excel导出方式。导出的源码demo下载下来后直接在tomcat中即可运行。源码:链接: 链接: http://pan.baidu.com/s/1bnD8mrd 密码: 59dh (有的时候百度网盘的链接会莫名其妙失效,如果该链接失效,请留言,我看到后会重发)。导出excel一般是:通过查询条件---选定特定的数据--点击导出。实现思路如下:在jsp页面首先需要加入:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><% response.setHeader("Content-disposition", "attachment; filename=BG.xls"); response.setHeader("Pragma", ""); response.setHeader("Cache-Control", "");%>
Then there is the list loop, with the following code:
<table> <thead> <tr> <TD>Serial number</td> <TD>Name</td> <TD>Gender</td> <TD>Age</td> <TD>ID number</td> </tr> </thead> <tbody> <c:foreach Items="${listperson}" var="User" Varstatus="status"> <tr> <TD>${STATUS.INDEX+1}</td> <TD>${user.name}</td> <TD>${user.sex}</td> <TD>${user.age}</td> <TD>${user.idcard}</td> </tr> </C:foreach> </tbody> </table>
Finally, the CSS style code for the table:
/ * table has a border * /Table,th,TD { border: 1px solid #000;} / * The text in the table is centered up or down * /TD { text-align: center; vertical-align: Middle; }/ * Show scientific notation in order to resolve export ID * /TD { mso-number-format: ' \@ ';}
In this way, the JSP can be exported in Excel, detailed reference to Excel exported demo.
JSP export Excel full instance plus source code