如何使用jquery确定系统上是否安装了Microsoft Excel或Open Office

时间:2013-04-10 06:04:37

标签: jquery excel openoffice.org

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

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

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

 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?

2 个答案:

答案 0 :(得分:0)

据我所知,存在模糊的方式, 您可以检查现有的Mime类型以确保存在

例如: 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);
            }

答案 1 :(得分:0)

我发布了一个问题,我是你的预期答案。这对你有所帮助。    Export HTML Table to EXCEL in Java script

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");

        }