//引入phpexcel类库
require_once “PHPExcel.php”;
//实例phpexcel
$objPHPExcel = new PHPExcel();
//设置第一个为活动报表
$objPHPExcel->setActiveSheetIndex(0);
//设置数据到表内
$objPHPExcel->getActiveSheet()->setCellValue(‘A2’, “设置A列第2行”)
->setCellValue(‘A3’, “设置A列第3行”)
->setCellValue(‘B3’, “设置B列第3行”)
;
$objPHPExcel->getActiveSheet()->setCellValue(‘A1’, “设置A列第1行”);
$objPHPExcel->getActiveSheet()->setCellValue(‘C3’, “设置C列第3行”);
//循环处理二维数组
$data=array(
‘0’=>array(‘name’=>’张三’,’age’=>’20’),
‘1’=>array(‘name’=>’李四’,’age’=>’30’),
);
//设置抬头
$objPHPExcel->getActiveSheet()->setCellValue(‘A5’, “姓名”)
->setCellValue(‘B5’, “年龄”)
;
$i=6;
foreach($data as $key=>$val){
$objPHPExcel->getActiveSheet()->setCellValue(‘A’.$i,$val[‘name’])
->setCellValue(‘B’.$i, $val[‘age’])
;
$i++;
}
//实例化保存类
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5’);
//保存路径,默认当前目录,注意目录权限
$objWriter->save(‘1.xls’);
//$objWriter->save(‘data/1.xls’);
//保存绝对地址
//$objWriter->save(‘D://1.xls’);