lishengzxc / bblog

My Blog
https://github.com/lishengzxc/bblog/issues
178 stars 8 forks source link

URL.createObjectURL(new Blob[...]) CSV 乱码问题 #30

Open lishengzxc opened 7 years ago

lishengzxc commented 7 years ago

http://blog.csdn.net/leonzhouwei/article/details/8447643

原因是 Excel 以 ANSI 格式打开,不会做编码识别。

解决方法

function downloadCSV(fileName, content) {
    let a = document.createElement('a');
    let file = URL.createObjectURL((new Blob(['\uFEFF' + content], { type: 'text/csv;charset=utf-8' })));

    a.href = file;
    a.download = `${fileName}.csv`;
    a.click();
}

http://stackoverflow.com/questions/19492846/javascript-to-csv-export-encoding-issue