PHP csv 函数,微软安全吗?

发布于 2024-09-12 06:07:55 字数 746 浏览 2 评论 0原文

我一直在研究 php fputcsv 函数,并听到一些评论说使用此方法创建的文件无法在 microsoft excel 上运行,这是正确的吗?

在 php.net 上发表评论

由于 PHP 通常是基于 *nix 的,因此 行结尾是有道理的 总是 \n 而不是 \r\n。然而, 某些 Microsoft 程序(我是 看着你,Access 97),将会失败 正确识别 CSV 除非 每行以 \r\n 结尾。其次,如果 第一列标题/值 CSV 文件以大写 ID 开头, 某些微软程序(咳咳, Excel 2007) 会将文件解释为 采用 SYLK 格式而不是 CSV,如下所述: http://support.microsoft.com/kb/323626

所以

如果我打算输出数据格式为

0、0278221234、60143512345、5pt代码,是
1、0278221234、60143512345、5分码,是
2、0278221234、60143512345、5分码,是
3、0278221234、60143512345、5pt代码,也是的

有什么区别?

使用此函数编写文件与简单地创建上面的行并以 \r\n 结尾

I have been looking into the php fputcsv function, and have heard some comments that the files created using this method will not work on microsoft excel, is this correct?

Comment on php.net

since PHP is generally *nix-based, it
makes sense that the line endings are
always \n instead of \r\n. However,
certain Microsoft programs (I'm
looking at you, Access 97), will fail
to recognize the CSV properly unless
each line ends with \r\n. Secondly, if
the first column heading / value of
the CSV file begins with uppercase ID,
certain Microsoft programs (ahem,
Excel 2007) will interpret the file as
being in the SYLK format rather than
CSV, as described here:
http://support.microsoft.com/kb/323626

So

If i am planning on outputting data in the format of

0, 0278221234, 60143512345, 5pt code, yes
1, 0278221234, 60143512345, 5pt code, yes
2, 0278221234, 60143512345, 5pt code, yes
3, 0278221234, 60143512345, 5pt code, yes

also,

what is the difference between writing a file using this function

and simply creating the line's as above and ending them with \r\n ?

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

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

发布评论

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

评论(2

嘦怹 2024-09-19 06:07:55

使用此方法创建的文件无法在 Microsoft Excel 上运行,这是正确的吗?

这是部分正确的。当用户使用导入向导时,MS Excel 可以很好地读取文件。但您会希望这些文件能够在用户不知道您如何制作它们的情况下运行——也就是说,不使用导入向导。

要使文件自动工作,请使用 ; (分号)作为记录分隔符。例如,

$data = array("one", "two", "three");
$fh = fopen('test.csv', 'w');
fputcsv($fh, $data, ";");
fclose($fh);

另请注意,为了将 UTF-8 写入 CSV,您必须将 UTF-8 BOM 添加到文件的开头。

files created using this method will not work on microsoft excel, is this correct?

This is partially correct. MS Excel can read the files fine when the user uses the import wizard. But you'll want that the files to work without user knowing how you've made them -- that is, without using the import wizard.

To make the files work automatically, use ; (semicolon) as a record separator. E.g.

$data = array("one", "two", "three");
$fh = fopen('test.csv', 'w');
fputcsv($fh, $data, ";");
fclose($fh);

Also note that in order to write UTF-8 to the CSV you'll have to add UTF-8 BOM to the beginning of the file.

骄兵必败 2024-09-19 06:07:55

如果您在使用 Excel 时遇到问题,您可以在创建文件后随时将 \n 替换为 \r\n。好像又多了 4 行代码。

[编辑]

刚刚使用 Excel 2007 进行了测试。打开正常。

IF you have problems with Excel, you can always replace \n with \r\n once the file is created. It's like 4 more lines of code.

[edit]

Just tested with Excel 2007. Opens fine.

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