导出为Excel中的所有浏览器

时间:2017-05-22 12:27:15

标签: javascript excel

我找到了脚本,但它并不适用于所有浏览器。我想用javascript。所以,我找到了下面的代码。如何在微软边缘和资源管理器中工作。

    <script type="text/javascript">
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<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"><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><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
</script>

1 个答案:

答案 0 :(得分:0)

我会告诉你一个简单的解决方案

在表格

的页面中添加一个按钮
<button class="btn btn-success">Export Data to Excel</button>

现在将以下给定的脚本添加到同一页面

<script src="js/jquery.table2excel.js"></script>
 <script>
      $(function() {
        $("button").click(function() {
           $("#tableID").table2excel({
             exclude: ".noExl",
             name: "Excel Document Name"
           });
        });
     });
</script>

不要忘记下载jquery.table2excel.js并将其添加到js文件夹