在 mysql 中使用 SELECT INTO OUTFILE 命令保存 XLS 文件
我对 XLS 文件使用 SELECT INTO OUTFILE 查询,因为
"SELECT * INTO OUTFILE " . "myfile.xls" . " FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM transactions where ppno like '%$cid%'
我使用 XLS 文件而不是 CSV。这有什么问题吗。当我尝试在 Microsoft Excel 中打开该 XLS 文件时,它显示“文件格式不同,您确定要打开吗”...请建议我
im using SELECT INTO OUTFILE query with XLS file as
"SELECT * INTO OUTFILE " . "myfile.xls" . " FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM transactions where ppno like '%$cid%'
Im using XLS file instead of CSV. Is there anything wrong with that. When i try to open that XLS file in Microsoft Excel it says 'The file is in different format, Are you sure you want to open'... Plz suggest me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Piskvor 和 Devart 所说,简单地为文件提供 .xls 扩展名实际上并不能使其成为 Excel 文件......生成的文件内容仍然只是一个制表符分隔值文件,正如您的 SQL 语句所表明的那样,
如果您想要从 PHP 脚本中创建一个真实 Excel 文件,您还可以(例如)格式化要导出的数据,那么您需要使用一个库,例如我自己的PHPExcel,或 此线程。
As Piskvor and Devart have said, simply giving a file an extension of .xls doesn't actually make it an Excel file.... the file content generated is still just a tab-separated value file as your SQL statement makes clear
If you want to create a real Excel file from within your PHP script, where you can also (for example) format the data that you're exporting, then you need to use a library such as my own PHPExcel, or one of the others listed in this thread.
是的,这确实有问题。也就是说,您仅更改文件名 - 这不足以自动将您的数据转换为适当的数据格式(换句话说,“任何其他名称的玫瑰都会闻起来很香”)。请注意,Excel 实际上在错误消息中告诉您这一点。
SELECT INTO OUTFILE
不支持 Excel 文档。您需要 a) 导出为 CSV,然后进行转换,或者 b) 获取具有此导出功能的工具(例如 phpMyAdmin 或 Navicat)。Yes, there is indeed something wrong with that. Namely, you are only changing the filename - that is not sufficient to automagically convert your data to the appropriate data format (in other words, "a rose by any other name would smell as sweet"). Note that Excel actually tells you this, in the error message.
SELECT INTO OUTFILE
has no support for Excel documents. You need to a) export into CSV, and convert afterwards, or b) get a tool which has this export capability (e.g. phpMyAdmin or Navicat).可以设置任何文件扩展名。实际上 xls 是 Excel 尝试读取的二进制 OLE 文件。
SELECT INTO OUTFILE 生成文本文件,尝试存储带有 CSV 或 TXT 扩展名的文件。在这种情况下,Excel 将正确打开文件。
It is possible to set any file extension. Actually xls is a binary OLE file that Excel tries to read.
SELECT INTO OUTFILE generates text file, try to store file with CSV or TXT extension. In this case Excel will open file correctly.