0

Im working on IE9, and I have the following code to download a html table as an excel spreadsheet:

<a id="toExcel" onclick="window.open('data:application/vnd.ms-excel,' + document.getElementById('resultsTable').outerHTML.replace(/ /g, '%20'));e.preventDefault();" href="javascript:void(0);">Excel</a>

This works in other browsers such as chrome, but not in IE9. It just opens up a new tab with the code in the url bar.

How do I get this to work in IE9?

I have tried putting the onclick event in another element such as a button it didnt work.

With datatables the library it seems it is adding a lot of other stuff to the table, i only want this feature.

Footnote for bounty:

  • Must work on IE9.

  • Please do not suggest https://datatables.net. For internal reason we cannot use this suggestion.

  • Am happy to vere from my what we have already tried (see code snippet earlier) as long as it works
  • Must download as xlsx and must open properly on excel 2010.
  • The project is using Sharepoint 2013 and a custom js/jquery webpart. We have dynamically generated a html table and need to be able to export to excel as above.

Thanks for any help

11
  • Side note: better replace that a tag with a button (button or input type="button" for correct semantics. Commented Jun 9, 2014 at 11:26
  • Have you thought about generating the Excel file on the server side? Depending on your server side technology, there may be a good library for generating Excel files. Commented Jun 11, 2014 at 15:13
  • Does compatibility view make any difference? Commented Jun 11, 2014 at 15:16
  • @BigChris have tested in both im afraid Commented Jun 11, 2014 at 15:52
  • 1
    @RSM: Can you go for server side solution? That will always work for all browsers. There are many APIs for excel generation for each language. Commented Jun 11, 2014 at 15:55

2 Answers 2

1
+150

As already mentioned a serverside implementation would be much better. Create an AJAX call to your controller with the appropriate id's included(needed to query your data) and send it back to the browser. If this is not possible in your situation please take a look at following post:

How to change "comma" separator to something else in blob csv?

I could copy my own answer but that doesn't make sense ;)

Sign up to request clarification or add additional context in comments.

Comments

0

Not exactly the solution of your problem but I also worked on "HTML table to Excel" report generation, I tried quite same code as yours, I also made this: http://jsfiddle.net/chankeypathak/7cwZ9/ but I kept facing issues like the fiddle I linked, there you see I used window.btoa method which I came to know later that it's not supported by IE9 or less. So I ended up using Datatables' TableTools plugin. There are a lot of options, export to CVS, Excel, PDF, copy to clipboard etc.

2 Comments

You might be able to get this to work by using David Chambers' window.btoa and window.atob polyfill
@AeroX: That's nice, however at that time I ended up generating it from server side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.