<?php $sales = array( array( 'Northeast', '2005-01-01', '2005-02-01', 12.54 ), array( 'Northwest', '2005-01-01', '2005-02-01', 546.33 ), array( 'Southeast', '2005-01-01', '2005-02-01', 93.26 ), array( 'Southeast', '2005-01-01', '2005-02-01', 945.21 ), array( 'All Regions', '--', '--', 1597.34 ), ); $output = fopen('php://output', 'w') or die("Can't open php://output"); $total = 0; //告诉浏览器发送的是一个csv文件 header("Content-type:application/csv"); header('Content-Disposition:attachment; filename="sales.csv"'); //输出表头 fputcsv($output, array('Region','Start Date','End Date','Amount')); //输出每一行数据,并递增 $total foreach ($sales as $sales_line) { fputcsv($output, $sales_line); $total += $sales_line[3]; } //输出全部数据行总和 fputcsv($output, array("All Regions", '--', '--', $total)); //关闭文件句柄 fclose($output) or die("can't close php://output");