简体   繁体   English

在php中编写csv文件时的空白行

[英]Blank line at the beginning when writing a csv file in php

I'm trying to write a csv file from an array, as a header of a csv file 我正在尝试从数组中编写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);

All the file is correct except a hidden character at the beginning of the file. 除了文件开头的隐藏字符外,所有文件都是正确的。 If I open the file with notepad it display correctly, but in Excel there is a blank line at the beginning. 如果我用记事本打开文件,它会正确显示,但在Excel中,开头有一个空行。 Can anyone help me? 谁能帮我?

It looks fine to me. 它看起来很好。 I tested with 我测试过

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

and I got no blank like or hidden character: 我没有空白或隐藏的角色:

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. 更新:因为这只发生在你而不是其他人身上,我相信这是因为你的php源文件中有一些换行符。 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);

Similar question has been been discussed here: 这里已经讨论过类似的问题:

How can I output a UTF-8 CSV in PHP that Excel will read properly? 如何在PHP中输出UTF-8 CSV,Excel将正确读取?

Look at the most upvoted solution which echoes out the result instead of writing to temporary file. 看看最赞成的解决方案,它回应结果,而不是写入临时文件。 It goes like this: 它是这样的:

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;

UPDATE: 更新:

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);

Use ob_clean(); 使用ob_clean(); right before $f = fopen('php://memory', 'w+'); 就在$f = fopen('php://memory', 'w+');之前$f = fopen('php://memory', 'w+');

Example: 例:

ob_clean();
$f = fopen('php://memory', 'w+');

It works fine for me to remove all blank lines at the begining of the CSV file. 我可以在CSV文件的开头删除所有空白行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM