解决Excel打开UTF-8编码的CSV文件乱码的问题


引用自:https://en.wikipedia.org/wiki/Comma-separated_values


CSV formats are not limited to a particular character set. They work just as well with Unicode character sets (such as UTF-8 or UTF-16) as with ASCII (although particular programs that support CSV may have their own limitations). CSV files normally will even survive naive translation from one character set to another (unlike nearly all proprietary data formats). CSV does not, however, provide any way to indicate what character set is in use, so that must be communicated separately, or determined at the receiving end (if possible).


Databases that include multiple relations cannot be exported as a single CSV file.


CSV是一个平面文件,它的编码方式有多种。比如,MongoDB以UTF-8格式存储数据,在使用mongoexport导出为CSV文件时,CSV的编码即为UTF-8。(可以将导出的CSV文件用UltraEdit打开来验证编码格式)


当直接使用Excel打开UTF-8编码的CSV文件时会出现乱码。


Microsoft Excel will open .csv files, but depending on the system's regional settings, it may expect a semicolon as a separator instead of a comma, since in some languages the comma is used as the decimal separator. It also applies some magic, such as reformatting what looks like numbers, eliminating leading + or 0, which breaks phone numbers, or a leading = makes the cell a formula, where function names must be in the opener's local language. Also, many regional versions of Excel will not be able to deal with Unicode in CSV. One simple solution when encountering such difficulties is to change the filename extension from .csv to .txt; then opening the file from an already running Excel instance with the "Open" command, where the user can manually specify the delimiters, encoding, format of columns, etc.


Excel打开CSV文件依赖于系统的区域设置。由于区域设置的影响,以及CSV文件不同的实现,那么Excel需要以正确的方式来打开CSV文件。我们以从MongoDB导出的UTF-8格式编码的CSV文件为例:


1. 打开Excel应用程序。


2. 点击“数据”菜单的“自文本”:

解决Excel打开UTF-8编码的CSV文件乱码的问题_第1张图片


3. 选择CSV文件,点击“导入”,出现“文本导入向导”对话框。

解决Excel打开UTF-8编码的CSV文件乱码的问题_第2张图片

从上面的默认设置我怀疑Excel默认以“Windows(ANSI)”打开CSV,未识别编码。


4. 文件类型选择“分割符号”,文件原始格式选择“65001:Unicode(UTF-8)”,勾选“数据包含标题”,点击下一步。

解决Excel打开UTF-8编码的CSV文件乱码的问题_第3张图片


5. 分割符号去掉“Tab键”而勾选“逗号”,点击“下一步”。

解决Excel打开UTF-8编码的CSV文件乱码的问题_第4张图片


6. 列数据格式根据需要选择,这里保持常规,点击“完成”。

解决Excel打开UTF-8编码的CSV文件乱码的问题_第5张图片


7. 弹出“导入数据”对话框,我们选择“现有工作表”,点击“确定”。

解决Excel打开UTF-8编码的CSV文件乱码的问题_第6张图片


8. 数据显示正常。