这个是第一钟方法:
public function exproOrderData($pre,$where)
{
$csv=new \Org\Util\Csv();
$logic = new \Financial\Model\EbayOrdersModel();
$result = array();
//这个是标题
$arr = array('','');
foreach ($pre as $key => $value) {
$list = $logic->getData($where,$value);
$result = array_merge($list,$result);
}
$csv->csvDownload($result,$arr);
}
public function csvDownload($data,$title)
{
$file_name="文件名字".date("y-m-d",time()).".csv";
header ( 'Content-Type: application/vnd.ms-excel' );
header ( 'Content-Disposition: attachment;filename='.$file_name );
header ( 'Cache-Control: max-age=0' );
$file = fopen('php://output',"a");
$limit=1000;
$calc=0;
foreach ($title as $v){
$tit[]=iconv('UTF-8', 'GB2312//IGNORE',$v);
}
fputcsv($file,$tit);
//如果是三维数组可以在这加一个foreach循环
foreach($data AS $key=>$j){
$calc++;
if($limit==$calc){
ob_flush();
flush();
$calc=0;
}
foreach ($j as $t){
$tarr[]=iconv('UTF-8', 'GB2312//IGNORE',trim($t));
}
fputcsv($file,$tarr);
unset($tarr);
}
unset($data);
fclose($file);
exit();
}
第二种
public function action_down(){
if(!empty($list)){
define('EOL',"\n");
$content = 'sku,品名,期初数,期初数,期初数,本期入库,本期入库,本期入库,本期出库,本期出库,本期出库,期末数,期末数,期末数'.EOL;
$content .= 'sku,品名,数量,单价,金额,数量,单价,金额,数量,单价,金额,数量,单价,金额'.EOL;
foreach($list as $v){
$content .= $v['sku'].',';
$content .= $v['sku_name'].',';
$content .= $v['quantity'].',';
$content .= $v['price'].',';
$content .= $v['total'].',';
$content .= $v['w_quantity'].',';
$content .= $v['w_price'].',';
$content .= $v['w_total'].',';
$content .= $v['d_quantity'].',';
$content .= $v['d_price'].',';
$content .= $v['d_total'].',';
$content .= $v['n_quantity'].',';
$content .= $v['n_price'].',';
$content .= $v['n_total'];
$content.=EOL;
}
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=test.csv");
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $content;exit;
}
}