$result=session("result");//获取值
$cout=count($result);//数组长度
$filename='simple.xls';//excel文件名字
ini_set('max_execution_time', '0');
Vendor('PHPExcel.PHPExcel');//引用phpexcel插件
$filename=str_replace('.xls', '', $filename).'.xls';
vendor('PHPExcel.PHPExcel');
$phpexcel = new \PHPExcel();//实例化
//设置excel属性
$phpexcel->getProperties()
->setCreator("Maarten Balliauw")//创建人
->setLastModifiedBy("Maarten Balliauw")//最后修改人
->setTitle("Office 2007 XLSX Test Document")//标题
->setSubject("Office 2007 XLSX Test Document")//题目
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")//描述
->setKeywords("office 2007 openxml php")//关键字
->setCategory("Test result file");//种类
for ($i=0;$i<$cout;$i++){
$phpexcel->getActiveSheet()->setCellValue("A".($i+2),$result[$i]["created_date"]);
$phpexcel->getActiveSheet()->setCellValue("B".($i+2),$result[$i]["effected_date"]);
$phpexcel->getActiveSheet()->setCellValue("C".($i+2),$result[$i]["policy_owner_chn"]);
$phpexcel->getActiveSheet()->setCellValue("D".($i+2),$result[$i]["policy_owner_eng"]);
$phpexcel->getActiveSheet()->setCellValue("E".($i+2),$result[$i]["insured_name"]);
$phpexcel->getActiveSheet()->setCellValue("F".($i+2),$result[$i]["agency_or_broker"]);
$phpexcel->getActiveSheet()->setCellValue("G".($i+2),$result[$i]["tr_person"]);
$phpexcel->getActiveSheet()->setCellValue("H".($i+2),$result[$i]["channel_manager"]);
$phpexcel->getActiveSheet()->setCellValue("I".($i+2),$result[$i]["channel_code"]);
$phpexcel->getActiveSheet()->setCellValue("J".($i+2),$result[$i]["wealth_mgr"]);
$phpexcel->getActiveSheet()->setCellValue("K".($i+2),$result[$i]["insurance_code"]);
$phpexcel->getActiveSheet()->setCellValue("L".($i+2),$result[$i]["policy_code"]);
$phpexcel->getActiveSheet()->setCellValue("M".($i+2),$result[$i]["insurance_no"]);
$phpexcel->getActiveSheet()->setCellValue("N".($i+2),$result[$i]["yearly_or_monthly"]);
$phpexcel->getActiveSheet()->setCellValue("O".($i+2),$result[$i]["currency"]);
$phpexcel->getActiveSheet()->setCellValue("P".($i+2),$result[$i]["premium"]);
$phpexcel->getActiveSheet()->setCellValue("Q".($i+2),$result[$i]["submit_month"]);
$phpexcel->getActiveSheet()->setCellValue("R".($i+2),$result[$i]["first_premium_month"]);
$phpexcel->getActiveSheet()->setCellValue("S".($i+2),$result[$i]["first_premium_hkd"]);
$phpexcel->getActiveSheet()->setCellValue("T".($i+2),$result[$i]["status"]);
$phpexcel->getActiveSheet()->setCellValue("U".($i+2),$result[$i]["yearly_premium"]);
$phpexcel->getActiveSheet()->setCellValue("V".($i+2),"");
$phpexcel->getActiveSheet()->setCellValue("W".($i+2),"");
$phpexcel->getActiveSheet()->setCellValue("X".($i+2),$result[$i]["responsible_person"]);
$phpexcel->getActiveSheet()->setCellValue("Y".($i+2),"");
$phpexcel->getActiveSheet()->setCellValue("Z".($i+2),$result[$i]["remark"]);
$phpexcel->getActiveSheet()->setCellValue("AA".($i+2),$result[$i]["work_industry"]);
$phpexcel->getActiveSheet()->setCellValue("AB".($i+2),$result[$i]["work_position"]);
$phpexcel->getActiveSheet()->setCellValue("AC".($i+2),$result[$i]["owner_insure_relationship"]);
$phpexcel->getActiveSheet()->setCellValue("AD".($i+2),$result[$i]["policy_owner_birthday"]);
$phpexcel->getActiveSheet()->setCellValue("AE".($i+2),$result[$i]["id_no"]);
$phpexcel->getActiveSheet()->setCellValue("AF".($i+2),$result[$i]["address"]);
}
$phpexcel->getActiveSheet()->setTitle('Sheet1');//设置sheet的name
$phpexcel->setActiveSheetIndex(0);//sheet的位置
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objwriter = \PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
$objwriter->save('php://output');
exit; 最佳答案