Generate csv file from javascript with special cha

2019-08-12 14:14发布

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

0条回答
登录 后发表回答