PHP CSV导出
导出php数据到csv文件
public function exportAction(){ $type= I('type'); // output headers so that the file is downloaded rather than displayed header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=data.csv'); $output = fopen('php://output', 'w'); if($type == 'starIndex'){ $starIndex = $this->RosterModel->getExportStarIndex($this->roster_edit_v); $title = array( '类型','对象ID','版本号','一星','二星','三星','四星','五星','总数','名字' ); fputcsv($output, $title); if($starIndex){ foreach ($starIndex as $row){ fputcsv($output, $row); } } } else if($type == 'forecast'){ $forecast = $this->RosterModel->getExportForecast($this->roster_edit_v); $title = array( '类型','对象ID','版本号','涨','平','跌','总数','名字' ); fputcsv($output, $title); if($forecast){ foreach ($forecast as $row){ fputcsv($output, $row); } } } }
解决中文excle乱码问题
解决bom echo “\xEF\xBB\xBF”;
<?php header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=data.csv'); $output = fopen('php://output', 'w'); $title = array( '联系地址','门店数量','所处行业 ','您的期望','E-MAIL','联系电话','公司名称','现在合作品牌' ); echo "\xEF\xBB\xBF"; fputcsv($output, $title); foreach ($db->SelectSQL($SQL) as $i => $value){ fputcsv($output, array( $value['1'], $value['2'], $value['3'], $value['4'], $value['5'], $value['6'], $value['7'], )); }