Reputation: 1656
I am trying to export data to excel in Javascript. Below is the code that I used:
var createAndExport = function(data){
var table = "<table><thead><tr><td>";
var head = Object.keys(data[0]);
for(var i=0;i<head.length;i++){
table += head[i]+"</td><td>";
}
table += "</td></tr></thead><tbody>";
for(var i=0;i<data.length;i++){
table += "<tr>";
for(var j=0;j<head.length;j++){
table += "<td>"+data[i][head[j]]+"</td>";
}
table += "</tr>";
}
table += "</tbody></table>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,'+ table;
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "data.xls";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
This code working fine and downloading the data as excel. But the issue is data containing arabic words, that is appearing as some other characters in dowloaded excel as in the image.
How can I fix this issue of arabic words?
Upvotes: 2
Views: 3154
Reputation: 61860
The encoding informations needs to be in the HTML
template too:
var createAndExport = function(data){
var table = "<head><meta charset='UTF-8'></head><table><thead><tr><td>";
...
See fiddle: https://jsfiddle.net/3fohpL9u/2/
Upvotes: 3