在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章,否则处于盲人摸象,毫无目的尴尬境地。本篇就是通过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>
下面是新版本的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>
<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"); }
<% if (exportToExcel == null) { %> <a href="excel.jsp?exportToExcel=YES">Export to Excel</a> <% } %>
如何把JSP页面导出到Excel中?,布布扣,bubuko.com
原文:http://blog.csdn.net/jptiancai/article/details/19967071