看到這個標題,會很疑問,java導出excel 有很多文章呀,都知道怎么用呀,呵呵,我也是這么認為,我們常用的就是jxl 或者poi ,現在jxl 代碼不在更新不支持2007,一般都采用poi 進行excel的操作,今天在看系統(架構struts+ spring+ hibernate),需要做一個excel導出,一個簡單的方法,這個方法只適用于excel簡單的導出,直接通過輸出的頁面,然后設置頁面為輸出流的文件格式,這樣當action轉向到jsp頁面的時候,直接就是將頁面輸出。
jsp頁面的代碼:
- <%@ page language="java" pageEncoding="UTF8"%>
- <html:html locale="true">
- <head>
- <title>LogExport</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <%
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application/vnd.ms-excel;");
- response.setHeader("Content-Disposition", "attachment;filename=\""+new String("Log.xls".getBytes(),"ISO-8859-1")+"\"");
- %>
- </head>
- <body>
- <form action="/Action_export.do">
- <table width="100%" border="1" cellspacing="0" cellpadding="0" id="datagrid">
- <thead>
- <tr class="HeaderStyle" style="font-weight: normal;">
- <th>用戶</th>
- <th>姓名</th>
- <th>日期</th>
- <th>登錄IP</th>
- <th>主機名</th>
- <th colspan="3">
- <table width='100%' border="1" cellspacing="3" cellpadding="0" frame='void' >
- <tr><td colspan="3" style='background:transparent;' align="center">操作信息</td></tr>
- <tr style='background:transparent;'>
- <td width="40px" align="center">操作</td>
- <td width="100px" align="center">標題</td>
- <td align="center">擴展信息</td>
- </tr>
- </table>
- </th>
- </tr>
- </thead>
- <jsp:include page="common.jsp" flush="true" />
- </table>
- </form>
- </body>
- </html:html>