本文介绍了tableToExcel jQuery在IE中抛出奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery函数将我的HTML表格导出到Excel。这是我在很多其他地方使用的功能,它在Chrome中适用于我:

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:

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))
            }
        })()

但是,在IE 10中,这一行:window.location.href = uri + base64(format(template,ctx)) - 抛出错误:SCRIPT122:传递给系统调用的数据区域太小。

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无法处理URI的长度。有没有解决方法?

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?

推荐答案

你的朋友遇到了大麻烦(开玩笑)!

You my friend are in big trouble ( kidding )!

旧版IE的主要问题是支持base64编码,可以使用。

The main issue with older versions of IE is the support for base64 encoding which can be taken care of using this Javascript library.

但主要问题是基于WebKit的浏览器上可用的数据URL方案,您可以在基于RCA 2397的IE上使用它。它的功能有限,主要是适用于图像和CSS。
是一个有用的链接。
另外,请务必阅读的维基百科页面

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.Here is a useful link.Also make sure to read the Wikipedia page for Data URL Scheme

**现在谈谈解决方法! **

**Now to talk about workarounds! **

主要解决方案是编写一个创建Excel文件的服务器端脚本,您只需使用Ajax调用将其发送回用户!您甚至可以将该文件存储在服务器的Filesystem
上,然后只发送该文件的地址。

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 Filesystemand just send the address of that file.

虽然如果你主要关注客户端并且你真的需要在IE上工作,你应该使用下载库,最好是那些有swf的(Flash支持)这里有一个我尚未使用的,虽然我希望得到你正在寻找的结果。有人声称做得很好,让我知道它是怎么回事!

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 downloadify does it well and let me know how it goes!

这篇关于tableToExcel jQuery在IE中抛出奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:18