2

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?

GarrettMurphy
  • 289
  • 4
  • 16
  • A practical limit for a URL/I (particularly when using IE) is about 2k characters. http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url – Tim Williams Feb 12 '13 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 '13 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 '13 at 19:47
  • Tim: I think you're onto something there. Time to fiddle and test. – GarrettMurphy Feb 12 '13 at 20:03

1 Answers1

2

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

GarrettMurphy
  • 289
  • 4
  • 16
  • I have a similar issue to solve. Using ASP.NET MVC4, I send back a transformed xml for Excel and want to display that in Excel. I have it working for Chrome and FF but cannot get IE or Safari to work. In Javascript (for FF/Chrome case), I the document.createElement("a") and sets its download to a filename and its href to a "data:....." uri. This pops up the Open/Save dialog and nearly all is well [with FF I Excel prompts about file format differs from extension]. – sheir Sep 19 '13 at 20:25
  • I have a similar issue to solve in ASP.NET MVC4/VS2012, I send back the xml (via ajax success function) and want to display that in Excel -- ie open the Open With/Save File dialog. I have it working for Chrome and FF [using "a" element with "download" attribute] but cannot get IE or Safari to work. For IE/Safari, I tried to do a window.open('data:....'). Nothing happens with Safari [not even an error]. I manage to get IE10 to open a new tab, but it only shows the data:application/vnd.ms-excel,.... in the address bar. Any help? – sheir Sep 19 '13 at 20:36
  • Posted my solution at: \ – sheir Sep 26 '13 at 19:21