PHP生成CSV单元格不会显示在窗口上


PHP Generate CSV Cell are not displaying on windows

我使用的是Cakephp 2.0。我需要生成一份excel格式的日常基础报告。所以我写了一个函数来生成一个CSV文件。当我在OpenOffice中下载文件时,它在Linux机器中运行良好,但如果我试图在windows Microsoft中打开相同的文件,则没有单元格,但数据显示正常。有了这个,我列出了我的编码

    $model.= "-".date('d M,Y');
    header ("Expires: Mon, ".date('d M,Y')." GMT");
    header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
    header ("Cache-Control: no-cache, must-revalidate");
    header ("Pragma: no-cache");
    header ("Content-type: application/vnd.ms-excel");
    header ("Content-Disposition: attachment; filename='"$model.xls" );
    header ("Content-Description: Generated Report" );

任何人都有助于乙醇

我认为您可能在这里做错了什么。请检查您的页眉。您在问题中提到了csv,并且您正在编写excel页眉。这可能会混淆MS Excel

header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename='"$model.xls" );
header ("Content-Description: Generated Report" );

我使用这种标题格式,它在windows和开放式办公室中都非常适合我。请试试这个。

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: octet/stream");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-disposition: attachment; filename=" . basename($filename.".csv"));
header("refresh: 0; url= ".$redirect_url);

Excel CSV文件应命名为SSV文件,因为数据列需要用分号而不是逗号分隔。此外,数据应该用引号括起来,任何内部引号都需要用双引号转义;例如CCD_ 2。