0

I have written the code in .JS file to fetch the data from sharepoint list and display in html table.

Now to take my work for next level I need to export the html table to "Excel". The below code is working fine in google chrome,But I find no luck in IE browser

Can any one help me for what reasons the below code is not getting execute

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $("#btnExport").click(function(e) {
    e.preventDefault();

    //getting data from our table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('table_wrapper');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');

    var a = document.createElement('a');
    a.href = data_type + ', ' + table_html;
    a.download = 'exported_table_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
    a.click();
  });
});
</script>
</head>

<body>
  <button id="btnExport">Export to xls</button>
  <br />
  <br />
  <div id="table_wrapper">
    <table border="1" cellspacing="0" bordercolor="#222" id="list">
      <tbody>
        <tr class="header">
          <th>user_id</th>
          <th>firstname</th>
          <th>lastname</th>
        </tr>
        <tr>
          <td>1</td>
          <td>Alex</td>
          <td>Lahevin</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Kostas</td>
          <td>Krevatas</td>
        </tr>
        <tr>
          <td>3</td>
          <td>Alexander</td>
          <td>Fakaris</td>
        </tr>
      </tbody>
    </table>
  </div>


</body>

</html>
  • Why build the html when you can directly build the Excel file? For example with: xlsx.js – Raoul Mensink Apr 12 '16 at 9:50
  • I am using the above JavaScript in my js.file .Using the html file for presenting in stack echange. My problem is with above javascript which is not working in IR browser – MSA Apr 13 '16 at 6:21
  • No the above code requíres the html otherwise you will get a empty Excel file. Which begs the qeustion why build the html and fetch when you can just build the Excel directly. – Raoul Mensink Apr 13 '16 at 6:57
  • can you please suggest is any alternative way to use in .js which helps in exporting the html table data in excel – MSA Apr 13 '16 at 8:00
  • can you use ActiveX? – IMTheNachoMan Apr 29 '16 at 21:06
2

a.download is not supported in IE and Mozilla browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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