user8579885
user8579885

Reputation:

Javascript export table to excel UTF-8 problem

my export to excel works like a charm, except there are no UTF-8 characters (Example : ÄÖÜÕ)

The code that i use :

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#c1292e'>";

    var textRange; var j=0;
    tab = document.getElementById('user_data'); // id of table

    for(j = 0 ; j < tab.rows.length ; j++) 
    {     
        tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text=tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table


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

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

I tried to add encoding to excel export, but sadly had no success.

tab_text = tab_text + '<head><meta charset="UTF-8" /><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

I also have encoding on tags.

any sugsestions? :).

Upvotes: 1

Views: 2935

Answers (1)

scraaappy
scraaappy

Reputation: 2886

Unfortunatly, escape is almost deprecated, but it's your faster solution : replacing

   sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

with

   sa = window.open('data:application/vnd.ms-excel,' + escape(tab_text));  

will do the trick.

It seems to be a problem with excel and encodeURIComponent Does VBA have any built in URL decoding? . You will find out other solutons here

Upvotes: 1

Related Questions