在IE9或更高版本中从Javascript导出excel或csv文件的任何方法

时间:2014-02-14 19:56:12

标签: javascript excel csv

我已经研究了这几天了,并且遇到了this one,它适用于Chrome和Firefox,但不适用于IE。我遇到this one from stackoverflow,据说是IE友好的,但在var objExcel = new ActiveXObject("Excel.Application");上窒息。我试过xlsx.js,但不能用flash。

我已经研究过CSV,但是在IE中遇到与URI相似的问题导致了窒息。

我正在努力保持客户端(因为我已经在本地存储了数据),但我必须使用IE 9+(因为我的主要用户将使用IE9 [不是我的选择])

任何解决方案,还是我为了这个而陷入服务器端?

1 个答案:

答案 0 :(得分:1)

不幸的是我无法访问IE9。这个解决方案适合您吗? http://bl.ocks.org/insin/1031969

如何设置data:网址,如下所示:

window.location.href = 'data:application/vnd.ms-excel;base64,PGh0bWwgeG1sbnM6bz0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTpvZmZpY2U6b2ZmaWNlIiB4bWxuczp4PSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpleGNlbCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnL1RSL1JFQy1odG1sNDAiPjxoZWFkPjwhLS1baWYgZ3RlIG1zbyA5XT48eG1sPjx4OkV4Y2VsV29ya2Jvb2s+PHg6RXhjZWxXb3Jrc2hlZXRzPjx4OkV4Y2VsV29ya3NoZWV0Pjx4Ok5hbWU+VzNDIEV4YW1wbGUgVGFibGU8L3g6TmFtZT48eDpXb3Jrc2hlZXRPcHRpb25zPjx4OkRpc3BsYXlHcmlkbGluZXMvPjwveDpXb3Jrc2hlZXRPcHRpb25zPjwveDpFeGNlbFdvcmtzaGVldD48L3g6RXhjZWxXb3Jrc2hlZXRzPjwveDpFeGNlbFdvcmtib29rPjwveG1sPjwhW2VuZGlmXS0tPjwvaGVhZD48Ym9keT48dGFibGU+PGNhcHRpb24+Q09ERS1QQUdFIFNVUFBPUlQgSU4gTUlDUk9TT0ZUIFdJTkRPV1M8L2NhcHRpb24+PGNvbGdyb3VwIG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90ZD48dGQ+PC90ZD48dGQ+PC90ZD48dGQ+WDwvdGQ+PC90cj48dHI+PHRkPjcwOTwvdGQ+PHRkPkFyYWJpYyAoQVNNTyA0NDkrLCBCQ09OIFY0KTwvdGQ+PHRkPjwvdGQ+PHRkPlg8L3RkPjx0ZD48L3RkPjx0ZD48L3RkPjx0ZD5YPC90ZD48L3RyPjx0cj48dGQ+NzEwPC90ZD48dGQ+QXJhYmljIChUcmFuc3BhcmVudCBBcmFiaWMpPC90ZD48dGQ+PC90ZD48dGQ+WDwvdGQ+PHRkPjwvdGQ+PHRkPjwvdGQ+PHRkPlg8L3RkPjwvdHI+PHRyPjx0ZD43MjA8L3RkPjx0ZD5BcmFiaWMgKFRyYW5zcGFyZW50IEFTTU8pPC90ZD48dGQ+PC90ZD48dGQ+WDwvdGQ+PHRkPjwvdGQ+PHRkPjwvdGQ+PHRkPlg8L3RkPjwvdHI+PC90Ym9keT48L3RhYmxlPjwvYm9keT48L2h0bWw+';

如果这不起作用,那么我认为你有两个选择:

  • 使用服务器端组件准备下载。这可以是一个非常简单的组件,它只是将GET参数作为文件返回。

  • 向包含数据的用户显示页面,并将其复制到Excel中。您可以使用制表符分隔的文本或HTML表格 - 其中一个应该很好地粘贴到电子表格中。