Javascript to csv export encoding issue

前端 未结 8 1372
Happy的楠姐
Happy的楠姐 2020-11-29 00:33

I need to export javascript array to excel file and download it I\'m doing it in this code. data is a javascript object array.

var csvContent = \"data:t         


        
8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 01:05

    Excel is really bad at detecting encoding, especially Excel on OSX.

    The best solution would be to encode your CSV in the default Excel encoding: windows-1252 (also called ANSI, which is basically a subset of ISO-8859-1).

    I put a complete example of how to do that at: https://github.com/b4stien/js-csv-encoding.

    The 2 main parts are stringencoding (to encode the content of your CSV in windows-1252) and FileSaver.js (to download the generated Blob).

    It looks like:

    var csvContent = 'éà; ça; 12\nà@€; çï; 13',
        textEncoder = new TextEncoder('windows-1252');
    
    
    var csvContentEncoded = textEncoder.encode([csvContent]);
    var blob = new Blob([csvContentEncoded], {type: 'text/csv;charset=windows-1252;'});
    saveAs(blob, 'some-data.csv');
    

提交回复
热议问题