vba 在 ms access 中使用 utf-8 编码导出文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22831137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exporting text file with utf-8 encoding in ms access
提问by kokosnakokos
I am exporting text files from 2 queries in ms access 2010. Queries are from different linked ODBC tables (but tables are different only by data, structure and data types are same). I set up export specification to export text file in utf-8 encoding for both files. Now here come the trouble part. When I export the queries and open them in notepad, one query is in utf-8 and second one is in ANSI. I don't know how is this possible when both queires has the same export specification and it is driving me crazy.
我正在从 ms access 2010 中的 2 个查询中导出文本文件。查询来自不同的链接 ODBC 表(但表仅因数据不同,结构和数据类型相同)。我设置了导出规范以导出两个文件的 utf-8 编码的文本文件。现在麻烦的部分来了。当我导出查询并在记事本中打开它们时,一个查询是 utf-8,第二个是 ANSI。我不知道当两个 queires 具有相同的导出规范时这怎么可能,这让我发疯。
This is my VBA code to export queries:
这是我导出查询的 VBA 代码:
DoCmd.TransferText acExportDelim, "miniflow", "qry01_CZ_test", "C:\TEST_CZ.txt", no DoCmd.TransferText acExportDelim, "miniflow", "qry01_SK_test", "C:\TEST_SK.txt", no
DoCmd.TransferText acExportDelim, "miniflow", "qry01_CZ_test", "C:\TEST_CZ.txt", no DoCmd.TransferText acExportDelim, "miniflow", "qry01_SK_test", "C:\TEST_SK.txt", no
I also tried to modify it by adding 65001 as coding argument by the results were same.
我还尝试通过添加 65001 作为编码参数来修改它,结果相同。
Do you have any idea what could be wrong?
你知道什么可能是错的吗?
回答by Gord Thompson
Don't rely on the File Open dialog in Notepad to tell you whether a text file is encoded as "ANSI" or UTF-8. That is just Notepad's "guess" based on whether the file begins with the bytes EF BB BF
, which is the UTF-8 Byte Order Mark (BOM).
不要依赖记事本中的“文件打开”对话框来告诉您文本文件是编码为“ANSI”还是 UTF-8。这只是记事本基于文件是否以 bytes 开头的“猜测” EF BB BF
,即 UTF-8 字节顺序标记 (BOM)。
Many (most?) Windows applications will include the UTF-8 BOM at the beginning of a text file that is UTF-8 encoded. Some Unicode purists insist, often quite vigorously, that the BOM is not required for UTF-8 files and should be excluded, but that is the way Windows applications tend to behave.
许多(大多数?)Windows 应用程序将在 UTF-8 编码的文本文件的开头包含 UTF-8 BOM。一些 Unicode 纯粹主义者通常非常坚决地坚持认为 UTF-8 文件不需要 BOM,应该将其排除在外,但这就是 Windows 应用程序的行为方式。
Unfortunately, Access does not always follow that pattern when it exports files to text. A UTF-8 text file exported from Access may omit the BOM and that can confuse applications like Notepad if they assume that a UTF-8 encoded file will always include the BOM as the first three bytes of the file.
不幸的是,Access 在将文件导出为文本时并不总是遵循这种模式。从 Access 导出的 UTF-8 文本文件可能会省略 BOM,如果记事本等应用程序假设 UTF-8 编码文件将始终包含 BOM 作为文件的前三个字节,则可能会混淆。
For a more reliable way of determining the encoding of a text file consider using an application like Notepad++to open the file. It will differentiate between the UTF-8 files witha BOM (which it designates as "UTF-8") and UTF-8 files withouta BOM (which it designates as "ANSI as UTF-8")
要确定文本文件编码的更可靠方法,请考虑使用Notepad++等应用程序打开文件。它将区分带有BOM的 UTF-8 文件(它指定为“UTF-8”)和没有BOM 的UTF-8 文件(它指定为“ANSI as UTF-8”)
To illustrate, consider the following Access table
为了说明,请考虑以下 Access 表
When exported to text (CSV) with UTF-8 encoding,
当以 UTF-8 编码导出为文本 (CSV) 时,
the File Open dialog in Notepad reports that it is encoded as "ANSI"
记事本中的文件打开对话框报告它被编码为“ANSI”
but a hex editor shows that it is in fact encoded as UTF-8 (the character é
is encoded as C3 A9
, not simply E9
as would be the case for true "ANSI" encoding)
但是十六进制编辑器显示它实际上被编码为 UTF-8(字符é
被编码为C3 A9
,而不是E9
像真正的“ANSI”编码那样简单)
and Notepad++ recognizes it as "ANSI as UTF-8"
Notepad++ 将其识别为“ANSI as UTF-8”
in other words, a UTF-8 encoded file without a BOM.
换句话说,一个没有 BOM 的 UTF-8 编码文件。