1

I want to download html table via jquery to excel file with utf-8 formatting. When the download is done, the special characters are not readable.

Here is my code (ajax function is commented, that code can work properly on jsfiddle): JSFiddle code

HTML Code

<div class="card border-primary mb-3">
  <table class="table table-hover results" id="exportExcel">
    <thead style="color: white; background-color: #0099CC">
      <tr>
        <td>Osobné číslo</td>
        <td>Meno</td>
        <td>Priezvisko</td>
        <td>Oddelenie</td>
        <td class="stockItem"><button type="button" id="excel" class="btn btn-sm btn-outline-light">stiahni zoznam</button></td>
      </tr>
    </thead>
    <tbody id="excelExport"></tbody>
    <!-- data dynamically loaded with ajax -->
      <tr>
        <td>1</td>
        <td>Ľudmila</td>
        <td>Malá</td>
        <td>vyr</td>
      </tr>
      <tr>
        <td>2</td>
        <td>Peter</td>
        <td>Hraško</td>
        <td>lg</td>
      </tr>
  </table>
</div>

jquery code

$(document).ready(function() {
  $("#excel").click(function(e) {
        // ajax field needed
        /*var stDate = $("#fromDate").val();
        var enDate = $("#toDate").val();*/

        //ajax function
        /*
        $.ajax({  
        type: 'POST',   
        url: 'downloadExcel.php',
        data: 'startDate='+stDate+'&endDate='+enDate, 
        success: function(data) {
            $('#excelExport').html(data);*/

            e.preventDefault();

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

            var a = document.createElement('a');
            a.href = data_type + ', ' + table_html;
            a.download = 'exported_table' + '.xls';
            a.click();
//}
//});
});
});

This in head:

<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">

I tried to find working code, but without any luck.

tomas
  • 121
  • 1
  • 2
  • 11
  • Excel? Are you aware that changing file extensions is not a universal mechanism to convert between computer data formats? – Álvaro González Jan 25 '18 at 11:35
  • I understand this question is about tricking Microsoft Office Excel into opening an HTML fragment as a spreadsheet. If so, I suggest you first start with a static HTML file (possibly a full HTML document and not just a random fragment so you can declare text encoding) in your desktop and play with it. It's too soon to care about JavaScript. – Álvaro González Jan 25 '18 at 11:39
  • I need to export dynamically generated data to excel file – tomas Jan 25 '18 at 12:05
  • You need to generate a true *.xls file that can be opened with any compliant software (such as LibreOffice Calc or Gnumeric) or you need to generate a file that Microsoft Office Excel can open? They're entirely different questions and the code you've shared attempts the accomplish the second goal. – Álvaro González Jan 25 '18 at 12:27
  • I need to generate file that MS Excel can open. Code which I shared is doing it, but the problem is that special characters looks like: Ä˝udmila Malá Peter Hraško – tomas Jan 25 '18 at 12:51
  • Then my original command about trying out static HTML stands. I'm not really sure of what additional information you're trying to provide, sorry. – Álvaro González Jan 25 '18 at 12:59
  • 1
    I found solution in this [post](https://stackoverflow.com/a/25730640/7617022) – tomas Jan 25 '18 at 19:28

0 Answers0