将jsp内容导出为Excel表简单实例
2016-10-10 17:05
274 查看
在网络上有很多关于jsp页面导出为Excel表格的例子,但好多是需要前台与后台相互关联实现的,我在这里的实例是只需要在jsp页面写代码即可实现,代码如下:
testExcel.jsp页面代码:
PS:当你点击“导出到excel”超链接的时候,所有页面的内容会被导出excel中。但是,我们可能不想让“导出到excel”的超链接出现在excel中。为了阻止它的出现,我们增加了一个判断条件,判断exportToExcel参数是否出现。如果出现,就意味着内容会被导出到excel中,而且不包括超链接。反之,就意味着我们只是想浏览器显示网页,那么超链接会出现在页面上。
testExcel.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:x="urn:schemas-microsoft-com:office:excel"> <script type="text/javascript"> function exportExcel(){ window.open('testExcel.jsp?exportToExcel=YES'); } </script> <head> <!-- 显示网格线 --> <xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>工作表标题</x:Name> <x:WorksheetOptions> <x:Print> <x:ValidPrinterInfo /> </x:Print> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml> <!-- 显示网格线 --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <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>ID</th> <th>文本内容</th> <th>序列</th> <td style="display: none">序列222</td> </tr> </thead> <tbody> <% for (int i = 0; i < 10; i++) { %> <tr bgcolor="lightblue"> <td align="center"><%=i%></td> <td align="center">文本内容 <%=i%></td> <td align="center"><%=i*10%></td> <td style="display: none" align="center"><%=i * 20%></td> </tr> <% } %> </tbody> </table> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <% if (exportToExcel == null) { %> <a href="javascript:exportExcel();">导出为Excel</a> <% } %> </body> </html>
PS:当你点击“导出到excel”超链接的时候,所有页面的内容会被导出excel中。但是,我们可能不想让“导出到excel”的超链接出现在excel中。为了阻止它的出现,我们增加了一个判断条件,判断exportToExcel参数是否出现。如果出现,就意味着内容会被导出到excel中,而且不包括超链接。反之,就意味着我们只是想浏览器显示网页,那么超链接会出现在页面上。
相关文章推荐
- Js 导出table内容到Excel的简单实例
- 将jsp内容导出为Excel表
- Js 导出table内容到Excel的简单实例
- 简单ajax应用实例:ajax+jsp实现用户名验证
- JSP 自定义标签之一 简单实例
- 直接把jsp中table内容导出到word或excel
- JSP只用Hashtable和session实现购物车功能的简单实例
- jsp文件上传简单实例
- 一个简单、方便的JSP验证码实例
- VB6做了个简单的ListView内容导出函数
- VB6做了个简单的ListView内容导出函数
- jsp导出Excel简单方式
- 一个简单在jsp页面中连接sqlserver数据库的小实例
- 小应用——jsp导出Excel简单报表
- 本文实现了一个基于servlet技术的简单的csv文件导出的程序实例。
- JSP自定义标签之一:简单实例
- jsp简单抓取页面内容
- Jsp中导出查询结果到excel文件的简单方法
- 简单jsp+servlet实例
- Oracle 导出实例全部内容