Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery将html表导出到xls不起作用 $(文档).ready(函数(){ $(“#btnExport”)。单击(函数(e){ window.open('data:application/vnd.ms excel,'+$('#dvDatatable').html()); e、 预防默认值(); }); });_Jquery_Html_Jquery Plugins_Export To Excel - Fatal编程技术网

使用jquery将html表导出到xls不起作用 $(文档).ready(函数(){ $(“#btnExport”)。单击(函数(e){ window.open('data:application/vnd.ms excel,'+$('#dvDatatable').html()); e、 预防默认值(); }); });

使用jquery将html表导出到xls不起作用 $(文档).ready(函数(){ $(“#btnExport”)。单击(函数(e){ window.open('data:application/vnd.ms excel,'+$('#dvDatatable').html()); e、 预防默认值(); }); });,jquery,html,jquery-plugins,export-to-excel,Jquery,Html,Jquery Plugins,Export To Excel,这是我用来导出html表的代码,其中#btnExport是我的导出按钮 “#dvDatatable”是我的表id。但这段代码对我不起作用。 它就像下载简单的文件一样 我必须对代码进行哪些更改?您可以使用此功能: <script> $(document).ready(function(){ $("#btnExport").click(function(e) { window.open('data:application/vnd.ms-excel,' + $('#dvData

这是我用来导出html表的代码,其中#btnExport是我的导出按钮 “#dvDatatable”是我的表id。但这段代码对我不起作用。 它就像下载简单的文件一样


我必须对代码进行哪些更改?

您可以使用此功能:

 <script>
 $(document).ready(function(){
 $("#btnExport").click(function(e) {

  window.open('data:application/vnd.ms-excel,' + $('#dvDatatable').html());
  e.preventDefault();
  });
  });
 </script>

参考文献:

它就像简单的文件一样下载……是xls格式还是文本格式。它是作为文本文件下载的。这方面运气好吗?这对任何人都有效吗?任何人都可以为上述答案提供完整的html+js代码。
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
            }
            var blob = new Blob([format(template, ctx)]);
            var blobURL = window.URL.createObjectURL(blob);
            return blobURL;
        }
})()
$("#btnExport").click(function () {
    var blobURL = tableToExcel('account_table', 'test_table');
    $(this).attr('download','download.xls')
    $(this).attr('href',blobURL);
});