用UTF-8 BOM通过Excel打开CSV。

时间:2023-01-05 18:56:13

I create csv file with data by the means of java. And I faced the following well-known issue: the letters in Portuguese were displayed by the wrong way in Excel (when opening by double click).

我用java的方法创建了csv文件。我遇到了一个众所周知的问题:在Excel中,用葡萄牙语写的字母用错误的方式显示(双击打开)。

I solved this by UTF-16LE+BOM, but excel started to recognize tabs as columns separators instead of commas.

我用UTF-16LE+BOM解决了这个问题,但是excel开始将制表符识别为列分隔符而不是逗号。

So I looked up for another solution and saw many posts, in which people say that just adding UTF-8 BOM and writing file in UTF-8 will do the job for Excel 2007 and later. I tried the simpliest sample on my work computer and it failed. But when I tried this at my home computer it worked like a charm.

所以我找了另一个解决方案,看到了很多帖子,人们说只要添加UTF-8 BOM和用UTF-8编写文件就可以完成Excel 2007和以后的工作。我在我的工作计算机上尝试了最简单的样本,但它失败了。但是,当我在家里的电脑上尝试这个的时候,它就像一种魔力。

Both computers have the same versions of java installed and operating system Windows 7. I am confused. Can anyone tell what can cause such a strange behaviour?

这两款电脑都安装了相同版本的java和操作系统Windows 7。我困惑。谁能说出是什么导致了这种奇怪的行为?

You can see my simpliest sample below:

你可以看到我最简单的例子:

String filename = "D:/check/test_with_bom.csv";
        FileOutputStream fos = new FileOutputStream(filename);
        byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }; 
        fos.write(bom);
        OutputStreamWriter osw = new OutputStreamWriter(fos , "UTF-8");
        PrintWriter printWriter = new PrintWriter(osw);

        printWriter.print("Hello,Olá,ão,ção");
        printWriter.close();

1 个解决方案

#1


4  

You should be aware that Excel does not "open" csv files. It converts them to an Excel file on the fly, using defaults. These defaults can be different depending on your regional settings. Because of that, it's never a good idea to let Excel open csv files using the defaults, since you'll never know for sure what you end up with.

您应该知道Excel不会“打开”csv文件。它动态地将它们转换为Excel文件,使用默认值。根据您的区域设置,这些缺省值可能会有所不同。正因为如此,使用默认值让Excel打开csv文件从来都不是一个好主意,因为您永远也不知道最终会得到什么。

A safer method is to use the 'import from text' method, and explicitly specify the delimiter, encoding, ect... Yet, be aware that 'save as csv' in Excel is an even worse idea, since it does not allow you to specify the encoding, delimiter, or any other detail. Access does.

更安全的方法是使用“从文本中导入”方法,并显式地指定分隔符、编码等。但是,请注意,在Excel中“save as csv”是一个更糟糕的想法,因为它不允许您指定编码、分隔符或任何其他细节。访问。

On American Windows versions of Excel, the default column separator is a comma. On European Windows versions the comma is reserved for the Decimal Symbol and to avoid conflicts, a semicolon is used by default as column separator.

在美国Windows版本的Excel中,默认的列分隔符是逗号。在欧洲Windows版本中,逗号保留为十进制符号,为了避免冲突,默认使用分号作为列分隔符。

If you -realy- -realy- -have- to use CSV, you can consider adding the "sep=," indicator at the top of your csv file. yet, be aware that this will probably cause problems in other applications.

如果您-realy- - - - - - -使用CSV,您可以考虑在CSV文件的顶部添加“sep=”指示器。但是,请注意,这可能会在其他应用程序中导致问题。

#1


4  

You should be aware that Excel does not "open" csv files. It converts them to an Excel file on the fly, using defaults. These defaults can be different depending on your regional settings. Because of that, it's never a good idea to let Excel open csv files using the defaults, since you'll never know for sure what you end up with.

您应该知道Excel不会“打开”csv文件。它动态地将它们转换为Excel文件,使用默认值。根据您的区域设置,这些缺省值可能会有所不同。正因为如此,使用默认值让Excel打开csv文件从来都不是一个好主意,因为您永远也不知道最终会得到什么。

A safer method is to use the 'import from text' method, and explicitly specify the delimiter, encoding, ect... Yet, be aware that 'save as csv' in Excel is an even worse idea, since it does not allow you to specify the encoding, delimiter, or any other detail. Access does.

更安全的方法是使用“从文本中导入”方法,并显式地指定分隔符、编码等。但是,请注意,在Excel中“save as csv”是一个更糟糕的想法,因为它不允许您指定编码、分隔符或任何其他细节。访问。

On American Windows versions of Excel, the default column separator is a comma. On European Windows versions the comma is reserved for the Decimal Symbol and to avoid conflicts, a semicolon is used by default as column separator.

在美国Windows版本的Excel中,默认的列分隔符是逗号。在欧洲Windows版本中,逗号保留为十进制符号,为了避免冲突,默认使用分号作为列分隔符。

If you -realy- -realy- -have- to use CSV, you can consider adding the "sep=," indicator at the top of your csv file. yet, be aware that this will probably cause problems in other applications.

如果您-realy- - - - - - -使用CSV,您可以考虑在CSV文件的顶部添加“sep=”指示器。但是,请注意,这可能会在其他应用程序中导致问题。