First method :user-defined
<%@ 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">
<!-- 命名x空间即调用excel里的属性-->
<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<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>导出excel文档</title>
</head>
<body>
<%
String exportToExcel = request.getParameter("exportToExcel");
String fileName = request.getParameter("fileName")==""?"excel":request.getParameter("fileName");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
//设置内容和头文件为excel形式
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","inline; filename="+fileName+".xls");
}
%>
<table align="left" border="2">
<thead>
<tr>
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%for (int i = 0; i < 10; i++) {%>
<tr>
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%}%>
</tbody>
</table>
<%
for (int i = 0; i < 10; i++) {
%>
<br>
<%}if (exportToExcel == null) {%>
<p>
<form action="" method="post">
<input type="hidden" name="exportToExcel" value="YES">
<!-- 自定义文件名,没有则默认excel.xls -->
请输入你要保存为EXCEL文档的文件名<input type="text" name="fileName" placeholder="如:excel">
<input type="submit" value="Export to Excel">
</form>
</p>
<%}%>
</div>
</body>
</html>
Second method :Using tableexport.js plug-in
<%@ 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">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>导出为Excel格式文件</title>
<link href="../static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link href="../static/dist/css/tableexport.min.css" rel="stylesheet">
<!--[if IE]>
<script src="http://libs.useso.com/js/html5shiv/3.7/html5shiv.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<br>
<h4>Countries By Population</h4>
<div class="table-responsive">
<table id="Population-list-1" class="table table-striped table-bordered"
data-name="cool-table">
<thead>
<tr>
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%for (int i = 0; i < 10; i++) {%>
<tr>
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%}%>
<tbody>
</table>
</div>
</div>
</div>
</div>
<!-- 获取文件名 -->
<input type="hidden" name="FileName" value="FileName" id="FileName">
<script src="http://libs.useso.com/js/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
<!-- 显示按钮插件 -->
<script>window.jQuery || document.write('<script src="../static/js/jquery-1.11.0.min.js"><\/script>')</script>
<script src="../static/bootstrap/js/bootstrap.min.js"></script>
<script src="../static/js/xlsx.core.min.js"></script>
<script src="../static/js/blob.js"></script>
<script src="../static/js/FileSaver.min.js"></script>
<script src="../static/dist/js/tableexport.js"></script>
<script type="text/javascript">
$(function(){
var FileName = window.document.getElementById('FileName').value;
$("table").tableExport({
headings: true,
footers: true,
formats:["xlsx","xls","csv","txt"],
fileName:FileName,
bootstrap:true
});
})
</script>
</body>
</html>
下载相关插件可参照该博主:http://www.jqueryfuns.com/resource/2381
注:当然方法多种多样,你也可以自己在方法一的基础之上调CSS样式等,若有不足之处,欢迎指点。