html中table导出Excel
2017-09-06 14:33
357 查看
有时候我们需要把网页中的数据导出excel格式来,那么我们用下面两种方法可以完成。
**
首先要先下载一个jquery.table2excel.js插件(网上搜搜),然后使用。
详情请浏览:html中table导出Excel
第一种.自写代码
<html> <head> <meta http-equiv="content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript"> function base64 (content) { return window.btoa(unescape(encodeURIComponent(content))); } /* *@tableId: table的Id *@fileName: 要生成excel文件的名字(不包括后缀,可随意填写) */ function tableToExcel(tableID,fileName){ var table = document.getElementById(tableID); var excelContent = table.innerHTML; var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>"; excelFile += "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>"; excelFile += "<body><table>"; excelFile += excelContent; excelFile += "</table></body>"; excelFile += "</html>"; var link = "data:application/vnd.ms-excel;base64," + base64(excelFile); var a = document.createElement("a"); a.download = fileName+".xlsx"; a.href = link; a.click(); } </script> </head> <body> <button type="button" onclick="tableToExcel('item','data')">导出</button> <table id="item"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <tr> <td>1</td> <td>小明</td> <td>19</td> </tr> <tr> <td>2</td> <td>小芳</td> <td>20</td> </tr> <tr> <td>3</td> <td>大军</td> <td>22</td> </tr> </table> </body> </html>
**
第二种.jquery插件
**首先要先下载一个jquery.table2excel.js插件(网上搜搜),然后使用。
<!doctype html> <html lang="zh"> <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>table2excel</title> <link rel="stylesheet" href="http://libs.baidu.com/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body> <header class="jq22-header"> <h4>table2excel-可将HTML表格内容导出到Excel中的jQuery插件 <span>jQuery Plugin to export HTML tabled to Excel Spreadsheet Compatible Files</span></h4> </header> <section class="jq22-container"> <div class="container" style="padding:30px 0"> <div class="row"> <div class="md-col-8"> <div class="table-responsive table2excel" data-tableName="Test Table 1"> <table class="table table-striped table-bordered table-hover"> <thead> <tr class="noExl"> <td class="danger">带<code>noExl</code>class的行不会被输出到excel中</td> <td class="danger">带<code>noExl</code>class的行不会被输出到excel中</td> </tr> <tr> <td class="success">这一行会被导出到excel中</td> <td class="success">这一行会被导出到excel中</td> </tr> </thead> <tbody> <tr> <td>单元格1-1</td> <td>单元格1-2</td> </tr> <tr> <td>单元格2-1</td> <td>单元格2-2</td> </tr> <tr> <td>单元格3-1</td> <td>单元格3-2</td> </tr> </tbody> <tfoot> <tr> <td colspan="2" class="warning">合并2个单元格</td> </tr> </tfoot> </table> </div> </div> </div> <button id="btn" class="btn btn-primary">点击这里将表格内容导出到excel中</button> </div> </section> <script src="http://www.jq22.com/jquery/1.11.1/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="js/jquery-1.11.0.min.js"><\/script>')</script> <script src="dist/jquery.table2excel.js"></script> <script> $(function() { $("#btn").click(function(){ $(".table2excel").table2excel({ exclude: ".noExl", name: "Excel Document Name", filename: "myFileName", exclude_img: true, exclude_links: true, exclude_inputs: true }); }); }); </script> </body> </html>
详情请浏览:html中table导出Excel
相关文章推荐
- 将HtmlTable内容导出到Excel,使用NPOI组件
- html中的table导出Excel
- 如何使用html表格自定义或采用tableexport.js第三方插件以excel格式导出
- 导出HTML的Table到Excel中(转载)
- html导出table支持(excel,png,word,pdf<pdf不太行,中文不支持>)
- 如何使用html表格自定义或采用tableexport.js第三方插件以excel格式导出
- java读取Excel导出Html对应的table代码
- 导出Excel的方法,传入为table或html!
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
- HTML Table 导出成Excel 边框变粗的问题
- 将html中table的内容导出为excel
- 【ASP.NET】C# 将HTML中Table导出到Excel(TableToExcel)
- 导出HTML的Table到Excel中
- 将html中table导出到excel xls文件的一种可用方法
- java读取Excel导出Html对应的table代码
- html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
- C# html的Table导出到Excel中
- javscript 导出html中的table到excel
- HTML Table 导出成Excel 边框变粗的问题