最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - tableToExcel jQuery throws strange error in IE - Stack Overflow

programmeradmin2浏览0评论

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-:office:office" xmlns:x="urn:schemas-microsoft-:office:excel" xmlns=""><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?

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-:office:office" xmlns:x="urn:schemas-microsoft-:office:excel" xmlns="http://www.w3/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?

Share asked Sep 4, 2013 at 17:09 JMax2012JMax2012 3732 gold badges6 silver badges25 bronze badges 1
  • solved here :- stackoverflow./questions/27039474/… – Alok Commented Dec 4, 2014 at 15:05
Add a ment  | 

1 Answer 1

Reset to default 4

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!

发布评论

评论列表(0)

  1. 暂无评论