如何使用html表格自定义或采用tableexport.js第三方插件以excel格式导出

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样式等,若有不足之处,欢迎指点。

转载于:https://my.oschina.net/u/3378039/blog/1560445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值