Javascript to csv export encoding issue

前端 未结 8 1362
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:27
     data=`"red","मुकेश"`
     var processdata = "data:text/csv;charset=utf-8,%EF%BB%BF" + encodeURIComponent(data);
    
    0 讨论(0)
  • 2020-11-29 01:28

    B4stien, thank you to you for your answer! After testing several solutions based on charset "utf8", encoding windows-1252 is the only solution that allowed me to keep my accent in Excel 365!

    Manetsus, the b4stien's answer and his link were very usefull for my case: i have to export french and german data into csv file: no solution based on "utf8" has worked... Only his solution which use an "ANSI" (window-1252) encoder...

    I give his code sample, and you can download the depending encoding-indexes.js, encoding.js and FileSaver.js from the link...

        <!doctype html>
        <html>
    
        <head>
            <meta charset="utf-8">
            <script type="text/javascript" src="encoding-indexes.js"></script>
            <script type="text/javascript" src="encoding.js"></script>
            <script type="text/javascript" src="FileSaver.js"></script>
        </head>
    
        <body>
            <a href="#" id="download-csv">Click me to download a valid CSV !</a>
    
            <script type="text/javascript">
                var csvContent = 'éà; ça; 12\nà@€; çï; 13',
                    textEncoder = new CustomTextEncoder('windows-1252', {NONSTANDARD_allowLegacyEncoding: true}),
                    fileName = 'some-data.csv';
    
                var a = document.getElementById('download-csv');
                a.addEventListener('click', function(e) {
                    var csvContentEncoded = textEncoder.encode([csvContent]);
                    var blob = new Blob([csvContentEncoded], {type: 'text/csv;charset=windows-1252;'});
                    saveAs(blob, fileName);
                    e.preventDefault();
                });
            </script>
        </body>
    
        </html>
    

    Nevertheless, as Excel is relatively open in the support of languages and formats, I do not exclude that UTF8 is not supported in my development environment because of the way it is installed ...

    Note: I test it with Firefox, Chrome and IE 11 on windows 7, with Excel 365...

    0 讨论(0)
提交回复
热议问题