使用方法:
$items=array();
$title=array("一号","二号");
for($i=0;$i<3;$i++){
$item=array();
for($ii=0;$ii<2;$ii++){
$item[]='你好'.(string)$ii;
}
$items[]=$item;
}
EchoExcel($title,$items);
/***
* @param $title 一维数组
* @param $data 多维数组
* @param string $filename 保存文件名不带后缀
* @return bool 失败返回false
*/
function EchoExcel($title,$data,$filename='excel'){
$top=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
if(!is_array($title) || count($title)==0){
return false;
}
if(!is_array($data) || count($data)==0){
return false;
}
vendor('phpoffice.phpexcel.Classes.PHPExcel');
$objPHPExcel = new \PHPExcel();
for($i=0;$i
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($top[$i].'1',$title[$i]);
}
for($i=0;$i
for($ii=0;$ii
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($top[$ii].($i+2),$data[$i][$ii]);
}
}
$objPHPExcel->getActiveSheet() -> setTitle('Sheet0');
$objPHPExcel-> setActiveSheetIndex(0);
$objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
ob_end_clean();//清除缓存以免乱码出现
header('Content-Type: application/vnd.ms-excel');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
$objWriter -> save('php://output');
}