Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We have reports generated in HTML that our users frequently open in Excel, which we serve simple enough by using the content-type. However, we've recently had a wrinkle popup: some of these reports use a lot of Javascript to conduct dynamic grouping/sorting, and the user wants to download this "finished" version of the report into Excel. I have a functioning Javascript function for that:

var uri = 'data:application/vnd.ms-excel;base64,';
var 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">';
template += '<head></head>';
template += '<body><table>{table}</table></body></html>';
var base64 = function(s) { 
    return window.btoa(unescape(encodeURIComponent(s)));
};
var format = function(s, c) { 
    return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; });
};
var ctx = {worksheet: name || 'Worksheet', table: daBody};
window.location.href = uri + base64(format(template, ctx));

I found that IE doesn't support window.btoa, so I addressed that, but now I get the following error when attempting to open the data in Excel:

The data area passed to a system call is too small.

I believe, despite the message, that this is caused by the base64 string in the URI being too long (I've seen in the past that IE doesn't accept extremely long URLs, and believe this is connected).

So, does anybody have any alternative method I can use for this in IE?

share|improve this question
A practical limit for a URL/I (particularly when using IE) is about 2k characters. stackoverflow.com/questions/417142/… – Tim Williams Feb 12 at 19:21
Correct...thus, the call for an alternative. I investigated ActiveX, but we serve a variety of web users, and insisting that they all allow ActiveX and add us to their Trusted List is asking a bit much. – GarrettMurphy Feb 12 at 19:46
As a workaround you could append a form to the DOM and use POST to send your table data to the client via the server. – Tim Williams Feb 12 at 19:47
Tim: I think you're onto something there. Time to fiddle and test. – GarrettMurphy Feb 12 at 20:03

1 Answer

Found a valid way of going about doing it:

  1. Used jQuery to create a form, gather all the HTML of the page (after Javascript completes) and post it to my server
  2. On my server, I created a function that takes in the form post and just returns it with the return type of "application/msexcel"
  3. Added script to run this Javascript on window.load of the report page

It's pretty hacky, but it works like a champ in all browsers

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.