Yii 导出excel

1. 首先在YII项目中导入phpexecl包


下载地址: phpexecl源码包


2. 开始使用

spl_autoload_unregister(array('YiiBase','autoload'));

$phpExcelPath = Yii::getPathOfAlias('ext.phpexcel'); //phpexcel跟目录
include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');	
include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel'. DIRECTORY_SEPARATOR .'IOFactory.php');
$objectPHPExcel = new PHPExcel();
$objectPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1','序号')
		->setCellValue('B1','第二列')
		->setCellValue('C1','第三列');
			
$objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(11);
$objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
$objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objectPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$row, $num)
->setCellValue('B'.$row, $record->nick)
->setCellValue('C'.$row, $record->send_date)
->setCellValue('D'.$row, $useStatus);
header('Content-Type : application/vnd.ms-excel');
header('Content-Disposition:attachment;filename=记录.xls"');//设置文件名
$objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
$objWriter->save('php://output');//输出


 
 


你可能感兴趣的:(execl)