1

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? :).

itaydafna
  • 1,968
  • 1
  • 13
  • 26
  • 1
    Since you're going the HTML table route, you could try generating character code HTML entities i.e. the `nnn;` format? But that isn't really an Excel file you're generating, it's just something that Excel is willing to open. – Rup Sep 04 '18 at 14:36
  • You are right, i just needed something with what i can export my html table. and that one does what i need - also is not for "mass use", it is in backend and does all the magic what i need :). –  Sep 05 '18 at 06:50

1 Answers1

1

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

Community
  • 1
  • 1
scraaappy
  • 2,830
  • 2
  • 19
  • 29