在 Firefox 浏览器中将动态 html 表导出到 javascript 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6955627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:57:05  来源:igfitidea点击:

Export dynamic html table to excel in javascript in firefox browser

javascriptexcelfirefox

提问by Siddharth

Want to Export dynamic html table to excel in javascript is there any way can i do do in firefox browser without using activex object in code .please help me

想要将动态 html 表导出到 javascript 中的 excel 有什么办法可以在 Firefox 浏览器中做而不在代码中使用 activex 对象。请帮助我

回答by Jonny Buchanan

Here's a functionfor doing this in Firefox with JavaScript, assuming the user has Excel installed on their machine:

这是在 Firefox 中使用 JavaScript 执行此操作的函数,假设用户在其计算机上安装了 Excel:

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))
  }
})()

jsFiddle live example:

jsFiddle 现场示例:

回答by Tan Piyapat

You can dynamically generate the Excel file in SpreadsheetDataXML format which allows you to custom the table, cell styles and format in HTML syntax.

您可以动态生成 SpreadsheetDataXML 格式的 Excel 文件,允许您自定义 HTML 语法中的表格、单元格样式和格式。

To make this work in IE you'll need to use Blob object and then call msSaveBlob method. For FF and Chrome, you can just change the data of href to data:application/vnd.ms-excel

要在 IE 中完成这项工作,您需要使用 Blob 对象,然后调用 msSaveBlob 方法。对于 FF 和 Chrome,您只需将 href 的数据更改为 data:application/vnd.ms-excel

function fnExcelReport() {
    var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
    tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

    tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';

    tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
    tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

    tab_text = tab_text + "<table border='1px'>";
    tab_text = tab_text + $('#myTable').html();
    tab_text = tab_text + '</table></body></html>';

    var data_type = 'data:application/vnd.ms-excel';

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        if (window.navigator.msSaveBlob) {
            var blob = new Blob([tab_text], {
                type: "application/csv;charset=utf-8;"
            });
            navigator.msSaveBlob(blob, 'Test file.xls');
        }
    } else {
        $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
        $('#test').attr('download', 'Test file.xls');
    }
}

Working example: http://jsfiddle.net/h42y4ke2/21/YT tutorial: https://www.youtube.com/watch?v=gx_yGY6NHkc

工作示例:http: //jsfiddle.net/h42y4ke2/21/YT 教程:https: //www.youtube.com/watch?v=gx_yGY6NHkc

回答by Thomas

AFAIK there's no library for creating a real excel file in JavaScript but you might try and export the table in HTML to a file with .xls extension.

AFAIK 没有用于在 JavaScript 中创建真正的 excel 文件的库,但您可以尝试将 HTML 中的表格导出到扩展名为 .xls 的文件中。

回答by Micha? ?rajer

There is an extension table2clipboardfor firefox. You can also generate csv output from DOM tree manually and let user save it as csv file. Excel can import from CSV.

Firefox有一个扩展table2clipboard。您还可以手动从 DOM 树生成 csv 输出,并让用户将其保存为 csv 文件。Excel 可以从 CSV 导入。