What's the best way to export UTF8 data into Excel?

前端 未结 8 1511
不思量自难忘°
不思量自难忘° 2020-12-03 03:12

So we have this web app where we support UTF8 data. Hooray UTF8. And we can export the user-supplied data into CSV no problem - it\'s still in UTF8 at that point. The proble

8条回答
  •  星月不相逢
    2020-12-03 03:27

    I fell on this post looking for the Ruby answer to why Excel wouldn't properly load a CSV with utf-8 characters. After searching around and experimenting this solution worked for me:

    csv_content = CSV.generate(col_sep: "\t", headers: :first_row, encoding: 'utf-8') do |csv|
      csv << ["header1", "header2"]
      csv << ["content1", "content2"]
    end
    write_content = Iconv.conv("utf-16le", "utf-8", "\xEF\xBB\xBF")
    write_content += Iconv.conv("utf-16le", "utf-8", csv_content)
    File.open("listing.csv", 'wb') {|f| f.write(write_content) }
    

提交回复
热议问题