1

How to you get this chrome API to download a CSV file that can be opened in Excel.

var csv = '123, 123, 美国'

chrome.downloads.download({
     url: 'data:attachment/csv;charset=UTF-8,' + encodeURI(csv),
     filename: name + '.csv',
     saveAs: true
}

The root cause of the difficulty is that CSV file does not have any notion of encoding built in. And Excel when encounters a file with extension CSV it assumes it is single byte order and opens it that way. If you have double byte characters encoded in UTF-8 in your file Excel opens that file with garbage characters.

The answers in this question suggest that adding BOM to for UTF-8 will clue in Excel that this is a UTF-8 file.

Microsoft Excel mangles Diacritics in .csv files?

However no matter what we tried we could not get Excel to recognize the file Automatically. Using text import wizard or other text editors it works.

Here is a tool that lets you try various combinations. But none of the worked for us: http://jsfiddle.net/kimiliini/HM4rW/show/light/

Community
  • 1
  • 1
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • Your fiddle code seems to insert the BOM just fine. If static files generated with other text editors do work, you should compare both files and see if there's a difference. – Álvaro González Aug 28 '15 at 09:15
  • 1
    Interesting problem. But the Fiddle example just works for me. If I put any characters under name, city, state (like those Chinese from your example or anything else really) and click download, then both under OS X with Apache Open Office Calc and under Linux with Libre Office Calc, I'm offered the the Text Import dialogue, and when selecting UTF-8 for encoding, the same characters appear in Excel. – marekful Aug 28 '15 at 09:16
  • I am running Office 2011 on OSX and BOM does not cause Excel to auto recognize the file. I tested it with HEX editors and no luck. – David Dehghan Aug 28 '15 at 10:25
  • 1
    Support for a UTF-8 BOM was added in Excel (Mac) 15. – 一二三 Aug 28 '15 at 13:29
  • UTF-16 LE with BOM works for me on Mac Excel 2011. Of course, you'll have to encode it first with a suitable encoder. – Alastair McCormack Aug 29 '15 at 09:19

0 Answers0