Generate csv file from javascript with special characters [duplicate]
Asked Answered
P

0

7

This the follow of this topic Here. Now I can generate and open a csv file. I just have a small problem with special characteres like €... When I open file with notepad++ : no problem. But under excel I've funny charactere... Same problem with file generate with IE, Chrome or FF. In notepad++, say encoding UTF-8. I think Excel try other encoding. Is it possible to force work with utf-8 for csv file or do i change encoding in my codes... I've no idea about good way to take.

UPDATE

I see methods to force Excel to open it in good format, but I need a methods more simple. It's an extract. When you click on button you don't want save on desk, Open Excel->Data... 10 clicks after see the good result. I search a method more automatic, you click, you see. Hoping is it possible???

UPDATE 2

And solution, but my question is wrongly put to duplicate. In fact, the solution is to add in javascript generator code a string to force Excel to open in UTF-8 directly. With this one and this other one I correct my code fir IE>9 and Chrome and FF (other i don't care know)

if(window.navigator.msSaveBlob){
    var blob = new Blob([new Uint8Array([0xEF,0xBB,0xBF]),result],{type:'application/csv;charset=UTF-8'});
    window.navigator.msSaveBlob(blob,filename);
}else if(window.webkitURL != null){
    var a=document.createElement("a");
    a.href="data:application/csv;charset=UTF-8,%EF%BB%BF" + encodeURIComponent(result);
    a.download=filename;
    a.click();
}

Just enjot it :-)

Platy answered 27/5, 2018 at 6:48 Comment(1)
Thanks a lot. I already wasted my 4 hours and you saved the next 40 I guess.Biller

© 2022 - 2024 — McMap. All rights reserved.