java通过jsp的Excel导出
在项目中一般导出报表用poi,可是假设你不想用框架就用简单的jsp也能够实现报表导出。并且实现起来还特别简单。
先看一下效果截图:
点击导出后的效果截图:
详细实现:
第一:在页面的列表页面中就是普通的iterator源代码例如以下:
<table class="list_tab"> <tr class="head"> <th>序号</th> <th width="20px"><input type="checkbox" class="inp02" style="vertical-align: middle;" id="sall"/></th> <th>体检编号</th> <th>档案编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>手机号码</th> <th>阳性项目结果</th> </tr> <s:iterator value="listsickpeople" status="s"> <tr class="row" onmouseover="this.className='overrow'" onmouseout="this.className='row'" align="center"> <td><s:property value="#s.index+1" /></td> <td><input type="checkbox" class="inp02" name="recordids" style="vertical-align: middle;" value="<s:property value="record_id" />"/></td> <td><s:property value="record_id" /></td> <td><s:property value="archive_id" /></td> <td><s:property value="real_name" /></td> <td align="center"><s:if test="sex==0"> <div class="sexw"></div> </s:if> <s:elseif test="sex==1"> <div class="sexm"></div> </s:elseif></td> <td><s:property value="age" /></td> <td><s:property value="contact_tel" /></td> <td align="left" title="<s:property value="is_yang_value" />"><s:property value="@com.hljw.util.UtilAPI@strBeyondHidden(is_yang_value,50)" /></td> </tr> </s:iterator> <s:if test="countTotal==0"> <tr class="tiprow"> <td colspan="9">没有找到符合条件的数据</td> </tr> </s:if> </table>
第二:点击导出后相应的action,就是将须要导出的list从数据库中查询出来。(没有特别的地方)
public String exportPositiveResult() { if (qureyBean == null) { qureyBean = new SickPeople(); } //这是将复选框的数组转化为sql的in条件 String[] record_ids = this.getParameterValues("recordids"); String record_id = stringArray2StringIn(record_ids); qureyBean.setRecord_id(record_id); listsickpeople = recordService.positiveresult(qureyBean, 1, 1000000); this.dictService.setDictName2ListData(listsickpeople, CacheGlobal.DICT_SEX); execlFileName = UncDate.formatDateTime(new Date(), "yyyyMMddHHmmss"); return SUCCESS; } /** * 将逗号隔开的数组转成In条件 * * @param str * @return */ public String stringArray2StringIn(String[] recordids) { StringBuffer idsStr = new StringBuffer(); for (int i = 0; recordids != null && i < recordids.length; i++) { if (i != 0) { idsStr.append(","); } idsStr.append("'").append(recordids[i]).append("'"); } return idsStr.toString(); }
第三:action运行后跳转的jsp。(这个特殊地方有两点:第一在头文件须要加一些语句。
第二页面仅仅须要导出报表须要的数据。没有不论什么js和css)源代码例如以下:
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags"%> <span style="color:#FF0000;"><% String execlFileName = (String)request.getAttribute("execlFileName"); response.setHeader("Content-disposition","attachment; filename="+execlFileName+".xls"); response.setHeader("Pragma", ""); response.setHeader("Cache-Control", ""); %></span> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>体检系统</title> </head> <body > <table border="1"> <tr> <th>序号</th> <th>体检编号</th> <th>档案编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>电话</th> <th align="left">阳性项目结果</th> </tr> <s:iterator value="listsickpeople" status="s"> <tr align="center"> <td><s:property value="#s.index+1" /> </td> <td><s:property value="record_id" /> </td> <td><s:property value="archive_id" /> </td> <td><s:property value="real_name" /> </td> <td><s:property value="sex" /> </td> <td><s:property value="age" /> </td> <td><s:property value="contact_tel" /> </td> <td align="left"><s:property value="is_yang_value" /> </td> </tr> </s:iterator> </table> </body> </html>