PHP:使用Windows编码创建CSV文件
我正在使用 php 创建 csv 文件。要将数据写入我的 csv 文件,我使用 php 函数“fputcsv”。
这就是问题所在: 我可以用Excel正常打开创建的文件。但我无法将文件导入商店系统(在本例中为“商店软件”)。它说“无法读取数据”。
现在线索来了:
如果我打开创建的文件并选择“另存为”并选择类型“CSV(逗号分隔)”,则可以将该文件导入到商店软件中。我读过一些关于 php 函数“mb_convert_encoding”的内容,我用它来编码数据,但它无法解决问题。
如果你能帮助我,我将非常高兴。
谢谢。
i am creating csv files with php. To write the data into my csv file, i use the php function "fputcsv".
this is the issue:
i can open the created file normally with Excel. But i cant import the file into a shopsystem (in this case "shopware"). It says something like "the data could not be read".
And now comes the clue:
If i open the created file and choose "save as" and select "CSV (comma delimited)" in type, this file can be imported into shopware. I read something about the php function "mb_convert_encoding" which i used to encode the data, but it could not fix the problem.
I will be very glad if you can help me.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的意见。
我通过用 fwrite 替换 fputcsv 解决了这个问题。然后我只需要将“\r\n”(感谢 wmil)添加到该行的末尾,生成的文件就可以被商店软件读取。
显然 fputcsv 函数使用 \n 而不是 \r\n 作为 EOL 字符。
Thanks for your input.
I solved this problem by replacing fputcsv with fwrite. Then i just needed to add "\r\n" (thanks wmil) to the end of the line and the generated file can be read by shopware.
Obviously the fputcsv function uses \n and not \r\n as EOL character.
我认为您无法使用 fputcsv 设置编码。然而,fputcsv 会查看区域设置,您可以使用 setlocale 进行更改。
也许您可以将文件直接发送到用户浏览器,并使用带有标头功能的更改内容类型和字符集。
I think you cannot set the encode using fputcsv. However fputcsv looks to the locale setting, wich you can change with setlocale.
Maybe you could send your file directly to the users browser and use changing contenttype and charset with header function.
如果不了解更多关于您的系统的信息,就无法回答这个问题。它很可能与字符编码无关。这可能是列数错误或列标题不正确的问题。
如果是字符编码问题,最好的选择是:
同时以 \r\n 结束换行符,而不仅仅是 \n。
如果这不起作用,您需要检查软件文档。
This can't be answered without knowing more about your system. Most likely it has nothing to do with character encoding. It's probably a problem with wrong number of columns or column headers being incorrect.
If it is a character encoding issue, your best bet is:
Also end newlines with \r\n, not just \n.
If that doesn't work you'll need to check the software docs.