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

时间:2013-11-23 23:13:37

标签: php csv fputcsv

我正在尝试从数组中编写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中,开头有一个空行。 任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

对我来说很好看。我用

进行了测试
$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

更新:因为这只发生在你而不是其他人身上,我相信这是因为你的php源文件中有一些换行符。确保标记之外没有任何内容,例如之前文件开头的换行符

您可以通过调用以下方法测试这是否是问题的原因:

ob_end_clean();

之前

fpassthru($f);

答案 1 :(得分:0)

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

How can I output a UTF-8 CSV in PHP that Excel will read properly?

查看最受欢迎的解决方案,它回应结果而不是写入临时文件。它是这样的:

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

答案 2 :(得分:0)

REPORT ZZZ. TABLES spfli. DATA: BEGIN OF wa, carrid TYPE spfli-carrid, connid TYPE spfli-connid, fldate TYPE sflight-fldate, bookid TYPE sbook-bookid, name TYPE stravelag-name, END OF wa, itab LIKE SORTED TABLE OF wa WITH UNIQUE KEY carrid connid fldate bookid. FIELD-SYMBOLS: <fs_spfli> TYPE spfli, <fs_wa> LIKE wa. ASSIGN: spfli TO <fs_spfli>, wa TO <fs_wa>. PARAMETERS: fromLoc LIKE <fs_spfli>-cityfrom, toLoc LIKE <fs_spfli>-cityto. SELECT p~carrid p~connid f~fldate b~bookid a~name INTO CORRESPONDING FIELDS OF TABLE itab FROM ( ( ( spfli AS p INNER JOIN sflight AS f ON p~carrid = f~carrid AND p~connid = f~connid ) INNER JOIN sbook AS b ON b~carrid = f~carrid AND b~connid = f~connid AND b~fldate = f~fldate ) INNER JOIN stravelag AS a ON b~agencynum = a~agencynum ) WHERE p~cityfrom = fromLoc AND p~cityto = toLoc AND f~seatsmax > f~seatsocc. LOOP AT itab INTO wa. AT NEW fldate. ULINE. WRITE: / . WRITE: 'FLIGHT' COLOR 4. ULINE. WRITE: / wa-carrid COLOR 3, wa-connid COLOR 3, wa-fldate COLOR 3. WRITE: / . WRITE: 'Booking ID' COLOR 3, 30 'Carrier ID' COLOR 3, 60 'Air Connection ID' COLOR 3, 90 'Travel Agency' COLOR 3. ULINE. ENDAT. WRITE: / wa-bookid, 30 wa-carrid, 60 wa-connid, 90 wa-name. ENDLOOP. 之前使用ob_clean();

示例:

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

对于我来说,删除CSV文件开头的所有空白行都很好。