phpexcel_simple.php /** * 导出excel * @param string $file_name 文件名 * @param int $type 类型,1积分活动,2计分,3商城 * @param string $title 文件顶部名称 * @param array $top_title 标题 * @param array $data 数据
/** * 导出excel * @param string $file_name 文件名 * @param int $type 类型,1积分活动,2计分,3商城 * @param string $title 文件顶部名称 * @param array $top_title 标题 * @param array $data 数据 * 前提是先下载phpexcel插件文件。此处是phpcms下使用,下载的phpexcel核心文件放在/phpcms/libs/classes中 */ function out_excel($file_name='',$type=1,$title,$top_title,$data){ $objPHPExcel = pc_base::load_sys_class('phpexcel'); $file_name = $file_name?$file_name:(date('YmdHi').'_'.random(4,'1234567890abcdefghijklmnopqrstuvw')); $title = $title?$title:$file_name; $top_title = array('ID','姓名','电话','当前状态','提交时间','变更时间'); $data = array( array('id'=>1,'nickname'=>'名1','mobile'=>'144454534534','status'=>'完成','createtime'=>'2017-12-22','modified'=>'2017-12-31'), array('id'=>2,'nickname'=>'名2','mobile'=>'144443534534','status'=>'未参加','createtime'=>'2017-12-22','modified'=>'2017-12-31'), ); $top_num = count($top_title) - 1; $letter =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'); $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$letter[$top_num].'1'); $objPHPExcel->getActiveSheet()->setCellValue("A1","$title"); for($i = 0;$i < ($top_num+1);$i++) { $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].'2',"$top_title[$i]"); } foreach ($data as $key => $value) { $i=$key+3;//表格是从1开始的 $objPHPExcel->getActiveSheet()->setCellValue("A".$i, $value['id']);//这里是设置A1单元格的内容 //以此类推,可以设置C D E F G看你需要了。 } //接下来当然是下载这个表格了,在浏览器输出就好了 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename='.$file_name.'.xls'); header("Content-Transfer-Encoding:binary"); $objWriter->save($file_name.'.xls'); $objWriter->save('php://output'); }