在php中生成csv文件

时间:2013-02-19 14:41:29

标签: php csv

我正在尝试从包含数据的php数组生成csv文件。我能够毫无问题地生成csv文件。但我很少在文件的标题上挣扎。

这是我的代码。

public function download_csv_file (){
    $total_call_log=get_overage_report();
    $start_date=date("Y-m-01");
    $end_date=date("Y-m-d");
    $filename="OveragesReport-$end_date";
    $heading="Bill Period From :$start_date To $end_date\n";
    $csv_output=$heading;
    $line = "Comp_ID,CompanyName,Cust_Name,Plan_Type,Plan_Limit,Plan_Cost,Plan_Overage,Total_Usages,Total_Overage_Cost\n";
    $csv_output .= $line;
    $comma = ",";
        foreach ($total_call_log as $call_info) {
                $line1 = NULL;
                $line1 .= $call_info['Comp_ID'];
    $line1 .= $comma.'"'.$call_info['CompanyName'].'"';
                $line1 .= $comma.'"'.$call_info['Cust_Name'].'"';
    $line1 .= $comma.'"'.$call_info['Plan_Type'].'"';
                $line1 .= $comma.'"'.$call_info['Plan_Limit'].'"';
    $line1 .= $comma.'"'.$call_info['Plan_Cost'].'"';
                $line1 .= $comma.'"'.$call_info['Plan_Overage'].'"';
    $line1 .= $comma.'"'.$call_info['total_call_length'].'"';
                $line1 .= $comma.'"'.ceil($call_info['total_overage_cost']).'"';
    $csv_output .= $line1."\n";
           }
    header('Content-type: application/octet-stream');
    header("Content-disposition: attachment; filename=".$filename.".csv");
    print($csv_output);
}

我想要的是,在第一行中只有所有列应该合并,并且标题应该在行的中间。 目前,标题($ heading)仅在第一列出现。我怎么能完成这个????

0 个答案:

没有答案