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.