2013-04-10 15 views
0

我需要将表格的数据导出到Excel。我得到了如下的解决方案:如何确定是否使用jquery在系统上安装了Microsoft Excel或Open Office

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

在上面的例子中,它可以让你产生xls文件时,安装在系统上的Microsoft Excel,但我有我的系统上安装了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(); 
}); 

ANOTHER编辑: 刚试过在IE8这个解决方案,这是行不通的。对于这个解决方案或此任何其他替代方案可以修复在IE上工作?

+0

你不能。这些信息不适用于JavaScript。 – JJJ 2013-04-10 06:07:24

+2

你最好的选择可能是问用户他喜欢哪种格式,我也不认为这是跨浏览器的方式 – hexblot 2013-04-10 06:09:33

+0

我认为这不可能用JavaScript。如果您不确定用户正在运行什么,请为不同应用程序的文件添加选项。 – 2013-04-10 06:15:58

回答

0

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

例如:办公 应用程序/ x-MSOFFICE

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); 
      } 
0

我发布的问题,我是你的预期answer.hope这个帮助你。 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"); 

     } 
+0

如果您可以在这里总结,您链接到的问题部分真的很棒这个问题。否则,目前这还没有真正回答原来的问题。 – DB5 2013-11-08 12:50:11

+0

@ DB5:谢谢你的忠告... – 2013-11-09 05:37:37

相关问题