5

I'm using this code above to export html table to excel file.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
    <meta charset="UTF-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script lang="js">
        function fnExcelReport() {
            var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
            tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

            tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';

            tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
            tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

            tab_text = tab_text + "<table border='1px'>";
            tab_text = tab_text + $('#myTable').html();
            tab_text = tab_text + '</table></body></html>';

            var data_type = 'data:application/vnd.ms-excel';

            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");

            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
                if (window.navigator.msSaveBlob) {
                    var blob = new Blob([tab_text], {
                        type: "application/csv;charset=utf-8;"
                    });
                    navigator.msSaveBlob(blob, 'Test file.xls');
                }
            } else {
                $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
                $('#test').attr('download', 'Test file.xls');
            }

        }
    </script>
</head>
<body>
    <a href="#" id="test" onClick="javascript:fnExcelReport();">download</a>

    <table id="myTable">
        <thead>
            <tr>
                <td style="padding-right: 30px;"><b>שם פרטי</b>

                </td>
                <td style="padding-right: 30px;"><b>גיל</b>

                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tester1</td>
                <td>30</td>
            </tr>
            <tr>
                <td>Tester2</td>
                <td>29</td>
            </tr>
            <tr>
                <td>Tester3</td>
                <td>17</td>
            </tr>
        </tbody>
    </table>
</body>

And it's work well, but when I'm type Hebrew characters so I have encoding issues..

How can I encode this file to UTF-8 encode that all the text will shown as it needs to?

Nir Gofman
  • 164
  • 1
  • 11
  • 5
    As you added `` to Main HTML file, did you try adding the same to the Head Element in HTML/Excel file using the variable `tab_text`? I.e. `tab_text = tab_text + '';` – Techie May 20 '16 at 08:58
  • 1
    Yes it works! thanks – Nir Gofman May 20 '16 at 09:06
  • Did you check the related threads? Answer was already present there http://stackoverflow.com/a/25730640/4879022 – Techie May 20 '16 at 09:27
  • Possible duplicate of [Encoding UTF-8 when exporting HTML table to Excel](http://stackoverflow.com/questions/25730008/encoding-utf-8-when-exporting-html-table-to-excel) – Techie May 20 '16 at 09:27

0 Answers0