4

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?

JMax2012
  • 353
  • 2
  • 6
  • 21
  • solved here :- http://stackoverflow.com/questions/27039474/how-to-export-datatables-table-to-excel-which-works-in-ie-as-well/27297238#27297238 – Alok Dec 04 '14 at 15:05

1 Answers1

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!

Community
  • 1
  • 1
pouyanghasemi
  • 447
  • 4
  • 8
  • Thanks for the very thorough answer! I will try some of these methods when I get back to this project next week, and will give further feedback from there. – JMax2012 Sep 07 '13 at 19:48
  • I still couldn't get it work, even with the new Javascript library. I ended up just writing two different export-to-excel functions (one using ActiveX) and calling them based on browser type. – JMax2012 Sep 12 '13 at 14:59
  • Actually I tried it and it worked! I used Downloadify from github. The code that I wrote will go live soon, I might be able to share the link with you. – pouyanghasemi Sep 18 '13 at 20:22
  • Thx for the answer, I used https://github.com/eligrey/FileSaver.js to fix the problem. See http://stackoverflow.com/a/20151856/2049986 to see how one changes a base64 string to a blob. – Jacob van Lingen Sep 17 '14 at 13:10