1
  <script type="text/javascript">
        $(document).ready(function () {
            //getting values of current time for generating the file name
            $(".toExcelButton").click(function(){
            var dt = new Date();
            var day = dt.getDate();
            var month = dt.getMonth() + 1;
            var year = dt.getFullYear();
            var hour = dt.getHours();
            var mins = dt.getMinutes();
            var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
            //creating a temporary HTML link element (they support setting file names)
            var a = document.createElement('a');
            //getting data from our div that contains the HTML table
            var data_type = 'data:application/vnd.ms-excel';
            var table_div = document.getElementById('dvData');
            var table_html = table_div.outerHTML.replace(/ /g, '%20');
            a.href = data_type + ', ' + table_html;
            //setting the file name
            a.download = 'exported_table_' + postfix + '.xls';
            //triggering the function
            a.click();
            //just in case, prevent default behaviour
            e.preventDefault();
                })
        });
    </script>

Need to export div tables to excel. The above code works fine in Chrome but not working in IE. Can anyone help me out on the same.

9
  • Do you have any errors? Also, on what version of IE does not "work"? Commented Dec 15, 2016 at 8:30
  • @lonut. No there aren't any errors. IE10+ Commented Dec 15, 2016 at 8:39
  • Then I suggest you use $(document).on('click', '.toExcelButton', function(){ // }) Commented Dec 15, 2016 at 8:41
  • @lonut. Tried the same as well but not working. Commented Dec 15, 2016 at 8:46
  • Can you post the HTML with the table values too? At least to make it a minimal working example. Commented Dec 15, 2016 at 8:47

2 Answers 2

3

In IE a dynamically created anchor tag needs to be added to the DOM to execute its click event. Furthermore the download attribute is not supported in the IE:

Download attribute on A tag not working in IE

Edit:

Recently I posted many answers handling this issue, here are two of those:

image does not download with it's own extension

JS Base64 string to downloadable pdf - Edge

Basically you have to use msSaveOrOpenBlob() in IE:

var tF = 'Whatever.xls';
var tB = new Blob(..);

if(window.top.navigator.msSaveOrOpenBlob){
    //Store Blob in IE
    window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
    //Store Blob in others
    var tA = document.body.appendChild(document.createElement('a'));
    tA.href = URL.createObjectURL(tB);
    tA.download = tF;
    tA.style.display = 'none';
    tA.click();
    tA.parentNode.removeChild(tA)
}

In the case above:

var tT = new XMLSerializer().serializeToString(document.querySelector('table')); //Serialised table
var tF = 'Whatever.xls'; //Filename
var tB = new Blob([tT]); //Blob

if(window.top.navigator.msSaveOrOpenBlob){
    //Store Blob in IE
    window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
    //Store Blob in others
    var tA = document.body.appendChild(document.createElement('a'));
    tA.href = URL.createObjectURL(tB);
    tA.download = tF;
    tA.style.display = 'none';
    tA.click();
    tA.parentNode.removeChild(tA)
}

https://jsfiddle.net/23ao1v0s/1/

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

8 Comments

Can you please help me on how i can achieve the same in IE?
Tired the same but no luck
@jinish shah: Any fiddle to see what was tried and did not work?
Actually missed out something. It's working fine now. Thanks a lot
@jinish shah: Alright. Please remember to accept/mark the answer if it provided what was looked for.
|
0

Please check the below given link. I think you will get a solution for your question https://github.com/rainabba/jquery-table2excel

1 Comment

In my case i am going to have multiple tables in a single div. For above plugin i had to specify id's to table, also each table will generate a seperate excel file. Where as i want all tables within single excel file.

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.