1

I want to export html table to excel and It works but sometimes Arabic or Persian data inside it was converted to something like below:

ع©ط§ط±ط¨ط± ط«ط¨طھ ع©ظ†ظ†ط¯ظ‡

I use this code:

var tableText = `<table><thead><tr><th colspan="5">یک عنوان در فارسی</th></tr><tr>`;
.
.
.
adding <th>s and <tbody> and <td>s
.
.
.
tableText += "</tbody></table>";
      var downloadLink = document.createElement("a");
      var uri = 'data:application/vnd.ms-excel,' + encodeURIComponent(tableText);
      downloadLink.href = uri;
      var filename = "some file name";
      downloadLink.download = filename + ".xls";

      document.body.appendChild(downloadLink);
      downloadLink.click();
      document.body.removeChild(downloadLink);

and it downloads excel file and it looks like the below image inside downloaded excel file:

enter image description here

Jalaleddin Hosseini
  • 2,142
  • 2
  • 25
  • 28
  • you can look for this answer could help you: [enter link description here](https://stackoverflow.com/questions/29355015/how-to-use-persian-or-arabic-numbers-in-html?lq=1) – Nemer Jul 07 '19 at 16:06

1 Answers1

5

I found a solution in the link: Encoding UTF-8 when exporting HTML table to Excel

and it works and all my unicode data saved correctly in the excel file. But I changed it a little because I wanted to use a given filename then I used an anchor tag instead of window.open, that dynamically added to the document and after clicking on, will be removed. Then it looks like below:

function exportExcel(tableText, filename, worksheetName) {
        let downloadLink = document.createElement("a");
        let uri = 'data:application/vnd.ms-excel;base64,'
            , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>' + tableText + '</body></html>'
            , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

        let ctx = { worksheet: worksheetName || 'Worksheet', table: tableText }
        // window.location.href = uri + base64(format(template, ctx));
        downloadLink.href = uri + base64(format(template, ctx));
        downloadLink.download = (filename||"exportedTable") + ".xls";

        document.body.appendChild(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);
    }

and finally you can call this function with your data:

exportExcel(anyHtmlTableText, "myFileName","mySheetName");
Jalaleddin Hosseini
  • 2,142
  • 2
  • 25
  • 28