使用MySQL into OUTFILE生成Excel可读的UTF8数据

发布于 2024-10-31 23:10:26 字数 1050 浏览 2 评论 0原文

我有一个类似于 这个问题。也就是说,我需要将 MySQL 数据库中的一些 UTF8 数据导出到 MS Excel。

Excel 提供的陷阱:

  • Excel 将 UTF8 格式的 CSV 文件作为 ANSCI 打开,从而破坏
  • Excel 正确打开制表符分隔的 UTF8 文件,但不支持换行符(我的数据有换行符,尽管在最坏的情况下我可能会失去这些)
  • Excel 显然,打开 UTF-16LE(小端)编码的 CSV 就可以了。但是,据我所知,MySQL INTO OUTFILE 不接受内容编码参数,而只是默认为数据库编码(UTF8)。

我的网络应用程序是 PHP 驱动的,但不幸的是我无法使用 PHP Excel 文件制作库,因为数据库非常大。我所有的导出都必须通过 MySQL 完成。

如果有人知道如何让 MySQL 在这方面超越 Excel,那就太好了。

非常感谢,

杰克

编辑这个 答案描述了适用于 Excel 2007 的解决方案。向文件添加“BOM”,我可以通过附加 BOM 的 PHP 脚本向客户端提供输出的文件来完成此操作。理想情况下,我希望找到一个在 2003 年也适用的解决方案。

I have a problem similar to this question. That is - I need to export some UTF8 data within a MySQL database to MS Excel.

The gotchas kindly Excel provides:

  • Excel opens UTF8 formatted CSV files as ANSCI, thus breaking
  • Excel will open tab-seperated UTF8 files correctly, but there is no support for linebreaks (my data has linebreaks, though in a worst-case scenario I might be able to loose these)
  • Excel will, apparently, open UTF-16LE (little endian) encoded CSVs OK. However, so far as I know, MySQL INTO OUTFILE does not accept content encoding argument, and just defaults to the database encoding (UTF8).

My web-app is PHP driven, but unfortunately I cannot use a PHP Excel-file-making library since the database is pretty large. All my exports must be done through MySQL.

If anybody knows how to make MySQL jump through Excel's hoops on this one, that would be great.

Many thanks,

Jack

Edit: This answer describes a solution that works for Excel 2007. Adding a 'BOM' to the file, which I may be able to do by providing the outputted file to the client via a PHP script that appends the BOM. Ideally I would like to find a solution that works in 2003 also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-11-07 23:10:26

为了处理 likebreak,我建议添加:

FIELDS ENCLOSED BY '"'

来自 mysql 文档的更完整示例

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;

For handling likebreaks I suggest adding:

FIELDS ENCLOSED BY '"'

A more complete example from mysql docs:

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;
请你别敷衍 2024-11-07 23:10:26

我记得在使用 Excel 时遇到过这个问题。 BOM 修复适用于 Excel 2007 和 2010。我们还希望支持 2003,但我们的解决方案是只编写 XLS 文件而不是 CSV 文件(使用 Java)。这听起来不适合您,因为您是从 MySQL 导出的。我想到的一个想法是在导出后将 UTF8 输出转换为 UTF-16LE。 此页面解释了如何使用 Perl 执行此操作。

I recall running into this issue with Excel. The BOM fix does work for Excel 2007 and 2010. We also wanted to support 2003, however our solution was to just write XLS files instead of CSV files (using Java). That doesn't sound like an option for you since you're exporting from MySQL. One idea that comes to mind is to convert your UTF8 output to UTF-16LE after your export. This page explains how to do it with Perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文