哪种编码可以在Mac和Windows上使用Excel正确打开CSV文件?

我们有一个Web应用程序,可以导出包含UTF-8外部字符的CSV文件,而不需要BOM。 Windows和Mac用户都在Excel中获取垃圾回收字符。 我试图用BOM转换成UTF-8; Excel / Win很好,Excel / Mac显示乱码。 我正在使用Excel 2003 / Win,Excel 2011 / Mac。 以下是我尝试的所有编码:

Encoding BOM Win Mac -------- --- ---------------------------- ------------ utf-8 -- scrambled scrambled utf-8 BOM WORKS scrambled utf-16 -- file not recognized file not recognized utf-16 BOM file not recognized Chinese gibberish utf-16LE -- file not recognized file not recognized utf-16LE BOM characters OK, same as Win row data all in first field 

最好的是带有BOM的UTF-16LE,但是CSV不能被识别。 字段分隔符是逗号,但分号不会改变事物。

有没有在两个世界都有效的编码?

Excel编码

我发现在处理Excel时, WINDOWS-1252编码是最令人沮丧的。 由于它基本上是微软自己的专有字符集,所以可以认为它可以在Mac和Windows版本的MS-Excel上工作。 两个版本都至less包含一个正确读取数据的相应“文件来源”或“文件编码”select器。

根据您的系统和您使用的工具,此编码还可以命名为CP1252ANSIWindows (ANSI)MS-ANSIWindows ,以及其他变体。

这个编码是ISO-8859-1 (也就是LATIN1和其他)的超集,所以如果你由于某种原因不能使用WINDOWS-1252 ,你可以回退到ISO-8859-1 。 请注意, ISO-8859-1缺lessWINDOWS-1252一些字符,如下所示:

 | Char | ANSI | Unicode | ANSI Hex | Unicode Hex | HTML entity | Unicode Name | Unicode Range | | € | 128 | 8364 | 0x80 | U+20AC | € | euro sign | Currency Symbols | | ‚ | 130 | 8218 | 0x82 | U+201A | ‚ | single low-9 quotation mark | General Punctuation | | ƒ | 131 | 402 | 0x83 | U+0192 | ƒ | Latin small letter f with hook | Latin Extended-B | | „ | 132 | 8222 | 0x84 | U+201E | „ | double low-9 quotation mark | General Punctuation | | … | 133 | 8230 | 0x85 | U+2026 | … | horizontal ellipsis | General Punctuation | | † | 134 | 8224 | 0x86 | U+2020 | † | dagger | General Punctuation | | ‡ | 135 | 8225 | 0x87 | U+2021 | ‡ | double dagger | General Punctuation | | ˆ | 136 | 710 | 0x88 | U+02C6 | ˆ | modifier letter circumflex accent | Spacing Modifier Letters | | ‰ | 137 | 8240 | 0x89 | U+2030 | ‰ | per mille sign | General Punctuation | | Š | 138 | 352 | 0x8A | U+0160 | Š | Latin capital letter S with caron | Latin Extended-A | | ‹ | 139 | 8249 | 0x8B | U+2039 | ‹ | single left-pointing angle quotation mark | General Punctuation | | Œ | 140 | 338 | 0x8C | U+0152 | Œ | Latin capital ligature OE | Latin Extended-A | | Ž | 142 | 381 | 0x8E | U+017D | | Latin capital letter Z with caron | Latin Extended-A | | ' | 145 | 8216 | 0x91 | U+2018 | ‘ | left single quotation mark | General Punctuation | | ' | 146 | 8217 | 0x92 | U+2019 | ’ | right single quotation mark | General Punctuation | | “ | 147 | 8220 | 0x93 | U+201C | “ | left double quotation mark | General Punctuation | | ” | 148 | 8221 | 0x94 | U+201D | ” | right double quotation mark | General Punctuation | | • | 149 | 8226 | 0x95 | U+2022 | • | bullet | General Punctuation | | – | 150 | 8211 | 0x96 | U+2013 | – | en dash | General Punctuation | | — | 151 | 8212 | 0x97 | U+2014 | — | em dash | General Punctuation | | ˜ | 152 | 732 | 0x98 | U+02DC | ˜ | small tilde | Spacing Modifier Letters | | ™ | 153 | 8482 | 0x99 | U+2122 | ™ | trade mark sign | Letterlike Symbols | | š | 154 | 353 | 0x9A | U+0161 | š | Latin small letter s with caron | Latin Extended-A | | › | 155 | 8250 | 0x9B | U+203A | › | single right-pointing angle quotation mark | General Punctuation | | œ | 156 | 339 | 0x9C | U+0153 | œ | Latin small ligature oe | Latin Extended-A | | ž | 158 | 382 | 0x9E | U+017E | | Latin small letter z with caron | Latin Extended-A | | Ÿ | 159 | 376 | 0x9F | U+0178 | Ÿ | Latin capital letter Y with diaeresis | Latin Extended-A | 

请注意欧元符号丢失 。 这个表格可以在Alan Woodfind。

转变

每种工具和语言的转换都是不同的。 但是,假设您有一个文件query_result.csv ,您知道它是UTF-8编码的。 使用iconv将其转换为WINDOWS-1252

 iconv -f UTF-8 -t WINDOWS-1252 query_result.csv > query_result-win.csv 

对于带有BOM的UTF-16LE,如果使用制表符作为分隔符而不是逗号,则Excel将识别这些字段。 它的工作原理是,Excel实际上最终使用其Unicode * .txtparsing器。

警告 :如果文件在Excel中编辑并保存,则会将其保存为制表符分隔的ASCII。 现在的问题是,当你重新打开文件Excel假定它是真正的CSV(用逗号),看到它不是Unicode,所以parsing它为逗号分隔 – 因此将做一个哈希!

更新 :至less在Excel 2010(Windows)中,上述警告在我今天似乎不会发生,尽pipe如果出现以下情况的确在保存行为方面似乎有所不同:

  • 您编辑并退出Excel(尝试保存为“Unicode * .txt”)

相比:

  • 编辑并closures文件 (按预期工作)。

内幕是:没有解决办法。 Excel 2011 / Mac无法正确解释包含变音符号和变音符号的CSV文件,无论您使用何种编码或箍环跳跃。 我会很高兴听到有人告诉我不同​​的!

您只能尝试使用逗号分隔和分号分隔的CSV。 如果您尝试使用制表符分隔的CSV(也称为TSV),则可以find答案:

带有BOM (字节顺序标记)的UTF-16LE制表符分隔


但是 :在一个评论中,你提到TSV不是你的select(虽然我还没能在你的问题中find这个要求)。 太可惜了。 这通常意味着您允许手动编辑TSV文件,这可能不是一个好主意。 目视检查TSV文件不是问题。 此外,编辑器可以设置为显示一个特殊的字符来标记标签。

是的,我在Windows和Mac上尝试过。

在Mac上使用UTF-8读取CSV文件的最佳解决方法是将它们转换为XLSX格式。 我find了一个由Konrad Foerstner制作的脚本,通过添加对不同分隔符的支持,我有所改进。

从Github下载脚本https://github.com/brablc/clit/blob/master/csv2xlsx.py 。 为了运行它,你将需要安装一个python模块openpyxl进行Excel文件处理: sudo easy_install openpyxl

以下是将utf8编码的CSV导入到Excel 2011 for Mac中的重要步骤:Microsoft说:“Mac版Excel目前不支持UTF-8。 Excel for Mac 2011和UTF-8

耶,去MS的路!

在我看来,Excel 2011 for Mac OS并没有像我想象的那样使用Encoding.GetEncoding(“10000”),而是浪费了2天的时间,但是和Microsoft操作系统一样。 最好的certificate是在Excel 2011中创build一个带有特殊字符的MAC文件,将其保存为CSV,然后在MAC文本编辑器中将其打开,并对字符进行encryption。

对我来说,这个方法是有效的 – 这意味着在MAC OS上的Excel 2011上的csv导出在西方欧洲有特殊的字符:

 Encoding isoMacOS = Encoding.GetEncoding("iso-8859-1"); Encoding defaultEncoding = Encoding.Default; // Convert the string into a byte array. byte[] defaultEncodingBytes = defaultEncoding.GetBytes(exportText); // Perform the conversion from one encoding to the other. byte[] ansiBytes = Encoding.Convert(defaultEncoding, isoMacOS, defaultEncodingBytes); decodedString = isoMacOS.GetString(ansiBytes); 

没有BOM的UTF-8当前在Excel Mac 2011中适用于我14.3.2。

UTF-8 + BOM的作品,但BOM呈现为乱码。

如果您导入文件并完成向导,UTF-16将起作用,但如果您只是双击它,则不起作用。

下面的Excel for Mac 2011和Windows Excel 2002为我工作:

  1. 在Mac上使用iconv,将文件转换为UTF-16 Little-Endian +将其命名为* .txt(.txt扩展名强制Excel运行文本导入向导):

    iconv -f UTF-8 -t UTF-16LE filename.csv >filename_UTF-16LE.csv.txt

  2. 在Excel中打开文件并在文本导入向导中select:

    • 第1步: 文件来源 :忽略它,你select什么并不重要
    • 第2步:为分隔符文本限定符select适当的值
    • 步骤3:如有必要,请select列格式

PS由iconv创build的UTF-16LE在开始时具有BOM字节FF FE。

PPS我的原始csv文件是在Windows 7计算机上以UTF-8格式创build的(使用BOM字节EF BB BF开头),并使用CRLF换行符。 逗号被用作字段分隔符和单引号作为文本限定符。 它包含ASCII字母,加上不同的拉丁字母,撇号,变音符号等等,还有一些西里尔字母。 在Win和Mac的Excel中都可以正常显示。

PPPS精确的软件版本:
* Mac OS X 10.6.8
* Excel for Mac 2011 v.14.1.3
* Windows Server 2003 SP2
* Windows Excel 2002 v.10.2701.2625

在我的Mac OS上,Text Wrangler将用Excel创build的CSV文件标识为“Western”编码。

一些谷歌search后,我做了这个小脚本(我不知道Windows的可用性,也许与Cygwin ?):

$ cat /usr/local/bin/utf8.sh

 #!/bin/bash INPUTFILE="$1" iconv -f macroman -c -t UTF-8 $INPUTFILE |tr '\r' '\n' >/tmp/file.$$.csv mv $INPUTFILE ms_trash mv /tmp/file.$$.csv $INPUTFILE 

在我的情况下,这工作(Mac,Excel 2011,西里尔文和拉丁字符与捷克的变音符号):

  • 字符集UTF-16LE(简单的UTF-16是不够的)
  • BOM“\ xFF \ xFE”
  • \ t(制表符)作为分隔符
  • 不要忘记编码分隔符和CRLFs 🙂
  • 使用iconv代替mb_convert_encoding

而不是csv,试图用XLS扩展名和“application / excel”MIMEtypes输出html。 我知道这将在Windows中工作,但不能为MacOS发言

这对我有用

  1. 在BBEdit或TextWrangler *中打开文件。
  2. 将该文件设置为Unicode(UTF-16 Little-Endian)(行结束符可以是Unix或Windows)。 保存!
  3. 在Excel中:数据>获取外部数据>导入文本文件…

现在关键点,selectMacIntosh作为文件来源(它应该是第一select)。

这是使用Excel 2011(版本14.4.2)

*窗口底部有一个下拉菜单

使用java(带有BOM的UTF-16LE)解决这个问题:

 String csvReportStr = getCsvReport(); byte[] data = Charset.forName("UTF-16LE").encode(csvReportStr) .put(0, (byte) 0xFF) .put(1, (byte) 0xFE) .array(); 

请注意,CSV文件应该使用TAB作为分隔符。 您可以在Windows和MAC OS X上读取CSV文件。

请参考: 如何使用BOM编码/解码UTF-16LE字节数组?

在我的情况下添加序言文件解决了我的问题:

 var data = Encoding.UTF8.GetBytes(csv); var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray(); return File(new MemoryStream(result), "application/octet-stream", "data.csv");