在 PHP 中编写 CSV 文件时,在开头留下空行

3

我正在尝试从一个数组中编写csv文件,作为csv文件的标题

$csv_fields[] = 'produto_quest';
$csv_fields[] = 'produto_quest1';
$csv_fields[] = 'comentario_aspecto_atm';
$csv_fields[] = 'aspecto_geral_int';
$csv_fields[] = 'organizacao_espaco';

$f = fopen('php://memory', 'w+');
fputcsv($f, $csv_fields, ";");

foreach ($relat as $fields) { // load from MySQL as a multidimensional array
    foreach($fields as $key => &$value1) {
        $value1 = iconv("UTF-8", "", $value1);
    }
    fputcsv($f, $fields, ";");
}
fseek($f, 0);
fpassthru($f);
fclose($f);

除了文件开头的一个隐藏字符外,所有文件都是正确的。 如果我使用记事本打开文件,它会正确显示,但在Excel中开头会有一行空白。 有人能帮我吗?

在问题标题中,您说空白行,但是接着又说隐藏字符。这是什么? - cen
我无法确定,因为我看不到导致 CSV 的第一行为空的原因,在记事本中似乎没有任何错误。 - RBrazao
3个回答

10
$f = fopen('php://memory', 'w+');之前使用ob_clean(); 示例:
ob_clean();
$f = fopen('php://memory', 'w+');

对我来说,将CSV文件开头的所有空行都删除是有效的。


5

我认为它看起来很好。 我进行了测试,

$relat = array(range(1,5), range(3,7));

我没有空格或隐藏字符:

HTTP/1.1 200 OK
Date: Sat, 23 Nov 2013 23:26:27 GMT
Server: Apache/2.4.6 (Ubuntu)
X-Powered-By: PHP/5.5.3-1ubuntu2
Vary: Accept-Encoding
Content-Length: 109
Content-Type: text/html

produto_quest;produto_quest1;comentario_aspecto_atm;aspecto_geral_int;organizacao_espaco
1;2;3;4;5
3;4;5;6;7

Update: Since this is happening only to you and not the others, I believe this is because you have some newline character in your php source file. Make sure there is nothing outside the marks, like a newline at the beginning of the file before

You can test if this is the cause of the issue by calling:

ob_end_clean();

right before

fpassthru($f);


您在第一个字符之前得到了一个空白行,就像我一样。 - RBrazao
我即使使用@fiLLLipnet的建议,仍然获得https://www.dropbox.com/s/ix20tp5xf17dldt/Capturar.PNG。 - RBrazao
如果你指的是produto_quest上面的空行,那是HTTP头和正文之间的标准空行。所有的HTTP响应都有这个空行。在它之后,正如你所看到的,就没有其他的空行了。 - Andrei B
请注意,它并没有解决实际问题(即<?php之外的额外字符)。尽管如此,您仍然可以使用它,但最好将该字符删除。 - Andrei B

0

类似的问题已经在这里讨论过:

如何在PHP中输出一个Excel可以正确读取的UTF-8 CSV?

看看最受欢迎的解决方案,它会回显结果而不是写入临时文件。它的代码如下:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $csv_file_content;

更新:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM

$csv_fields[] = 'produto_quest';
$csv_fields[] = 'produto_quest1';
$csv_fields[] = 'comentario_aspecto_atm';
$csv_fields[] = 'aspecto_geral_int';
$csv_fields[] = 'organizacao_espaco';

$f = fopen('php://memory', 'w+');
fputcsv($f, $csv_fields, ";");

foreach ($relat as $fields) { // load from MySQL as a multidimensional array
    foreach($fields as $key => &$value1) {
        $value1 = iconv("UTF-8", "", $value1);
    }
    fputcsv($f, $fields, ";");
}
fseek($f, 0);
fpassthru($f);
fclose($f);

你建议使用echo输出文件而不是使用fputcsv吗? - RBrazao
我刚才看了一下其他的建议,但是我发现你可以将你的代码粘贴到我建议的代码的最后一行。那应该可以工作 :) 我已经更新了我的建议。 - fiLLLipnet
通过这个回声,我会在文件的开头或结尾得到“”,具体取决于回声位置。 - RBrazao
如果我使用更新的代码,在Excel中看起来是这样的:https://db.tt/xy3QM237 在Notepad++中是这样的:https://db.tt/9RvUoq47 - fiLLLipnet
BOM标记应该是文件的第一个字符。如果我的回答中的更新是正确的,那么您的文档可能有一些空格字符(来自php源代码),然后是BOM,然后是CSV数据。这就是为什么BOM显示为常规字符的原因。 - Andrei B
如果我将我的代码,仅仅是我的代码粘贴到一个新的php文件中,它就可以工作。你的完整代码是什么样子的?在粘贴的代码之前,它是否输出了其他内容? - fiLLLipnet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接