Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - tableToExcel jQuery throws strange error in IE

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

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."

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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! **

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.

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...