在此,強調一下搜索時關鍵詞的重要性,這樣一下子可以定位到文章,否則處於盲人摸象,毫無目的尷尬境地。本篇就是通過export jsp to excel找到的。
原文地址:How to Export Web Page to Excel (in JSP)?
本篇教程我們會看到如何把JSP頁面導出到Excel中,會在已有的JSP頁面中增加導出excel的功能。
許多時候對於用戶來說,可以在excel中看到頁面內容是很方便的。公共的方案會被導出成包含一些報告、數字等信息的表格。通過導出數據導出到excel中,最終用戶也可以使用excel來做各種的分析,這一點對於你的java基本程序來實現,是有困難的。
假設這就是你的jsp頁面:

這是對應的jsp源碼(導出excel功能還沒有加)。一個包含簡單數據表格的jsp頁面。
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Export to Excel - Demo</title>
- </head>
- <body>
- <table align="center" border="2">
- <thead>
- <tr bgcolor="lightgreen">
- <th>Sr. No.</th>
- <th>Text Data</th>
- <th>Number Data</th>
- </tr>
- </thead>
- <tbody>
- <%
- for (int i = 0; i < 10; i++) {
- %>
- <tr bgcolor="lightblue">
- <td align="center"><%=i%></td>
- <td align="center">This is text data <%=i%></td>
- <td align="center"><%=i * i%></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- </body>
- </html>
我們會添加一個“導出到excel”的超鏈接,它會把頁面內容導出到excel文件中。那么這個頁面會變成這個樣子:

下面是新版本的jsp源碼。這個版本增加了“導出到excel”超鏈接,而且增加了相應的功能:
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Export to Excel - Demo</title>
- </head>
- <body>
- <%
- String exportToExcel = request.getParameter("exportToExcel");
- if (exportToExcel != null
- && exportToExcel.toString().equalsIgnoreCase("YES")) {
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("Content-Disposition", "inline; filename="
- + "excel.xls");
- }
- %>
- <table align="left" border="2">
- <thead>
- <tr bgcolor="lightgreen">
- <th>Sr. No.</th>
- <th>Text Data</th>
- <th>Number Data</th>
- </tr>
- </thead>
- <tbody>
- <%
- for (int i = 0; i < 10; i++) {
- %>
- <tr bgcolor="lightblue">
- <td align="center"><%=i + 1%></td>
- <td align="center">This is text data <%=i%></td>
- <td align="center"><%=i * i%></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
- <%
- if (exportToExcel == null) {
- %>
- <a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
- <%
- }
- %>
- </body>
- </html>
導出網頁到excel代碼解釋:
- <a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
- String exportToExcel = request.getParameter("exportToExcel");
- if (exportToExcel != null
- && exportToExcel.toString().equalsIgnoreCase("YES")) {
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("Content-Disposition", "inline; filename="
- + "excel.xls");
- }
3) 當你點擊“導出到excel”超鏈接的時候,所有頁面的內容會被導出excel中。但是,我們可能不想讓“導出到excel”的超鏈接出現在excel中。為了阻止它的出現,我們增加了一個判斷條件,判斷exportToExcel參數是否出現。如果出現,就意味着內容會被導出到excel中,而且不包括超鏈接。反之,就意味着我們只是想瀏覽器顯示網頁,那么超鏈接會出現在頁面上。
- <%
- if (exportToExcel == null) {
- %>
- <a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
- <%
- }
- %>
導出頁面到excel的顯示
注意:
如果沒有設置:response.setHeader("Content-Disposition", "attachment; filename=" + filename+".xls");
則默認為當前頁面.xls。
比如testexcel.jsp--->則生成testexcel.xls表格
擴展閱讀:
[1]在JSP中通過http協議生成excel和word:作者用過jxl之后,選擇了http方式生成excel
[2]JSP-EXCEL save defualt in .xls extension, open excel with in the browser:
[3]利用HTTP協議,更改輸出文件:引出了HTTP1.1協議的重要性
[4]超文本傳輸協議-HTTP(修訂版):詳細解釋了HTTP1.1協議
[5]Multiple tables data exported to multiple worksheets of the same excel sheet in java:一個excel中,存儲在多個sheet
