javascript tableToExcel jQuery 在 IE 中引发奇怪的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18619902/
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
tableToExcel jQuery throws strange error in IE
提问by JMax2012
I am using a jQuery function to export my HTML table to Excel. This is a function I have seen used in plenty of other places, and it works fine for me in Chrome:
我正在使用 jQuery 函数将我的 HTML 表导出到 Excel。这是我在很多其他地方看到的一个函数,它在 Chrome 中对我来说很好用:
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))
}
})()
However, in IE 10, this line: "window.location.href = uri + base64(format(template, ctx))" - is throwing the error: "SCRIPT122: The data area passed to a system call is too small."
但是,在 IE 10 中,这一行:“window.location.href = uri + base64(format(template, ctx))” - 抛出错误:“SCRIPT122:传递给系统调用的数据区域太小。”
I've done a bit of research and it seems like IE is not able to handle the length of the URI for some reason. Are there any workarounds?
我做了一些研究,似乎 IE 由于某种原因无法处理 URI 的长度。有什么解决方法吗?
回答by pouyanghasemi
You my friend are in big trouble ( kidding )!
你我朋友麻烦大了(开玩笑)!
The main issue with older versions of IE is the support for base64 encoding which can be taken care of using this Javascript library.
旧版本 IE 的主要问题是对 base64 编码的支持,这可以使用这个 Javascript 库来解决。
But the main issue is the Data URL Scheme which is available on WebKit based browsers and you can have it on IE based on RCA 2397. It has a limited functionality and mostly works for images and css. Hereis a useful link. Also make sure to read the Wikipedia page for Data URL Scheme
但主要问题是数据 URL 方案,它在基于 WebKit 的浏览器上可用,您可以在基于 RCA 2397 的 IE 上使用它。它的功能有限,主要适用于图像和 css。 这是一个有用的链接。还要确保阅读维基百科页面的数据 URL 方案
**Now to talk about workarounds! **
**现在谈谈解决方法!**
The main solution is writing a server side script that creates the Excel file and you just use an Ajax call to send it back to the user! you can even store that file on your server's Filesystem and just send the address of that file.
主要的解决方案是编写一个创建 Excel 文件的服务器端脚本,您只需使用 Ajax 调用将其发送回用户即可!您甚至可以将该文件存储在服务器的文件系统上,然后发送该文件的地址。
Though if you are mostly focusing on the client side and you really need to work on IE, you should use a download library, preferably the ones that have swf (Flash support) here is one that I have not used yet, though I am hoping to get the result that you are looking for. There are claims that downloadifydoes it well and let me know how it goes!
虽然如果你主要关注客户端并且你真的需要在 IE 上工作,你应该使用一个下载库,最好是那些有 swf(Flash 支持)的库是我还没有使用过的,尽管我希望以获得您正在寻找的结果。有人声称downloadify做得很好,让我知道它是怎么回事!