下载excel \ n和\ t时无效

时间:2015-03-05 09:37:16

标签: php

我编写了下载excel文件的代码。它是一个给予标题和数据。但我的标题没有在标签中分开。它只在一个标签中(在一列中)。请参考代码。

$res = mysql_query($sql);

    $header = "Sr No. \t Team Member Name \t Profile Name \t Email Id. \t Guys Count \tGirls Count \tNo.paid guys \tNo.paid girls \t Guest Count";


    while ($row_gl = mysql_fetch_assoc($res))
    {

            $sr_no = str_replace('"', '""', $count);
            $sr_no = '"' . $sr_no . '"' . "\t";
            $line .= $sr_no;

            if (empty($row_gl['ds_comments']))
            {
                $gl_count = str_replace('"', '""', "NA");
                $gl_count = '"' . $gl_count . '"' . "\n";
                $line .= $gl_count;
            }
            else
            {
                $gl_count = str_replace('"', '""', $row_gl['ds_comments']);
                $gl_count = '"' . $gl_count . '"' . "\n";
                $line .= $gl_count;
            }

            $count++;    
    }
    $name = date('d-m-y');
    //header("Content-type: application/octet-stream");

    header("Content-type:application/vnd.ms-excel;name='excel'");

    header("Content-Disposition: attachment; filename=".$name. ".xls");
    header("Pragma: no-cache");
    header("Expires: 0");

    // Output data
    echo $header . "\n\n" . $data;

1 个答案:

答案 0 :(得分:0)

您是否考虑过用逗号分隔并创建CSV?

就我个人而言,我会使用现有的PHP to Excel库或CSV之类的文件。