1

我需要将表格的数据导出到 Excel。我得到了如下解决方案:

http://jsfiddle.net/jquerybyexample/xhYcD/

在上面的示例中,当系统上安装了 Microsoft Excel 但我的系统上安装了 OpenOffice 时,它​​允许您生成.xls文件,所以当我添加该行时

 window.open('data:application/vnd.oasis.opendocument.spreadsheet,' + $('#dvData').html());

代替

 window.open('data:application/vnd.ms-excel,' + $('#dvData').html());

然后它会生成 .ods 文件。

我需要确定是否安装了 Excel 或 Open Office,以便我可以使用 jQuery 或 JavaScript 设置上述两行的条件。


编辑

我可以把这两个条件背靠背,那是有害的吗?

$("#btnExport").click(function(e) {
    window.open('data:application/vnd.oasis.opendocument.spreadsheet,' + $('#dvData').html());
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
    e.preventDefault();
});

另一个编辑

刚刚在IE8上试过这个解决方案,它不起作用。此解决方案的任何其他替代方案或可以修复以在 IE 上工作?

4

2 回答 2

0

As far as i know there exists a vague way , you can check the existing Mime types to ensure the presence

eg: application/x-msoffice for office

function GetMimeTypes () {
                var message = "";
                    // Internet Explorer supports the mimeTypes collection, but it is always empty
                if (navigator.mimeTypes && navigator.mimeTypes.length > 0) {
                    var mimes = navigator.mimeTypes;
                    for (var i=0; i < mimes.length; i++) {
                        message += "<b>" + mimes[i].type + "</b> : " + mimes[i].description + "<br />";
                    }
                }
                else {
                    message = "Your browser does not support this ";
                   //sorry!
                }

                return ( message);
            }
于 2013-04-10T06:33:33.767 回答
0

我已经发布了我是您的预期答案的问题。希望这对您有所帮助。 在 Java 脚本中将 HTML 表导出到 EXCEL

function downloadsalesreport () {

            var cache = {};

            this.tmpl = function tmpl(str, data) {
                // Figure out if we're getting a template, or if we need to
                // load the template - and be sure to cache the result.
                var fn = !/\W/.test(str) ?
                  cache[str] = cache[str] ||
                    tmpl(document.getElementById(str).innerHTML) :

                  // Generate a reusable function that will serve as a template
                  // generator (and which will be cached).
                  new Function("obj",
                    "var p=[],print=function(){p.push.apply(p,arguments);};" +

                    // Introduce the data as local variables using with(){}
                    "with(obj){p.push('" +

                    // Convert the template into pure JavaScript
                    str.replace(/[\r\t\n]/g, " ")
                          .split("{{").join("\t")
                          .replace(/((^|}})[^\t]*)'/g, "$1\r")
                          .replace(/\t=(.*?)}}/g, "',$1,'")
                          .split("\t").join("');")
                          .split("}}").join("p.push('")
                          .split("\r").join("\\'")
                          + "');}return p.join('');");

                // Provide some basic currying to the user
                return data ? fn(data) : fn;
            };
 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>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</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 (tableList, name) {
                    if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById("#tablesalesentry")
                    var tables = [];
                    for (var i = 0; i < tableList.length; i++) { tables.push(tableList[i].innerHTML); }
                    var ctx = { worksheet: name || 'Worksheet', tables: tables };
                    window.location.href = uri + base64(tmpl(template, ctx))
                }
            })();

            tableToExcel(document.getElementsByTagName("table"), "one");

        }
于 2013-11-08T12:27:10.280 回答