本文主要是介绍ThinkPHP3.2 + PHPExcel 进行execl文件导出操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本次是进行execl导出实例的讲解,
1、首先下载PHPExcel扩展类 (可以百度 没有的找我要157400661@qq.com)
2、在ThinkPHP引入PHPExcel 那么我是放到了Vendor中
3、然后就可以开心的去写控制器方法了
注意要这样写去引用哦 $excel = new \PHPExcel();
因为我是导出excel 先保存到服务器然后再将导出的文件和其他文件一起进行压缩后下载的(文件合并压缩后续我会再写文档介绍) 所以这段代码最后ajax返回的操作可以不用管
如果大家想直接导出后就下载 可以参考最后注释的几行
/*** 真正的导出* @param array $complain_info 投诉信息结构体* $is_download_mv 是否下载视频* @return [type] [description]*/public function export_complain($complain_info,$is_download_mv){Vendor('PHPExcel.PHPExcel','','.class.php');//创建对象$excel = new \PHPExcel();$excel->getActiveSheet()->setTitle('投诉列表');// 设置单元格高度// 所有单元格默认高度$excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);// 第一行的默认高度$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);// 垂直居中$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);// 设置水平居中$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//Excel表格式$letter = array('A','B','C','D','E','F','F','G','H','I','J','K','L');//表头数组$tableheader = array('序号','车牌号','车牌颜色','投诉说明','投诉地点','投诉人','投诉时间','投诉人手机号','投诉人微信号','微信昵称','状态','进展','奖励');$tablestyle = array(array('width'=>'5'),array('width'=>'15'),array('width'=>'10'),array('width'=>'40'),array('width'=>'30'),array('width'=>'10'),array('width'=>'15'),array('width'=>'15'),array('width'=>'15'),array('width'=>'15'),array('width'=>'10'),array('width'=>'10'),array('width'=>'10'),);// 如果选择视频下载if($is_download_mv){$letter[] = 'M';$tableheader[] = '视频地址(相对路径)';$tablestyle[] = array('width'=>'30');}// id , plate_num,color,msg,place,name,time,phone,weixin,wx_name,status,process//填充表头信息for($i = 0;$i < count($tableheader);$i++) {$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");$excel->getActiveSheet()->getColumnDimension($letter[$i])->setWidth($tablestyle[$i]['width']);}//填充表格信息for ($i = 2;$i <= count($complain_info) + 1;$i++) {$data = $complain_info[$i - 2];if($is_download_mv){$this->mv_arr[] = $data['vedio'];$tmp = explode('/',$data['vedio']);$tmpurl = array_pop($tmp);$excel->getActiveSheet()->setCellValue("$letter[13]$i","{$tmpurl}");}$excel->getActiveSheet()->setCellValue("$letter[0]$i","{$data['id']}");$excel->getActiveSheet()->setCellValue("$letter[1]$i","{$data['plate_num']}");$excel->getActiveSheet()->setCellValue("$letter[2]$i","{$data['color_str']}");$excel->getActiveSheet()->setCellValue("$letter[3]$i","{$data['msg']}");$excel->getActiveSheet()->setCellValue("$letter[4]$i","{$data['place']}");$excel->getActiveSheet()->setCellValue("$letter[5]$i","{$data['name']}");$excel->getActiveSheet()->setCellValue("$letter[6]$i","{$data['time']}");$excel->getActiveSheet()->setCellValue("$letter[7]$i","{$data['phone']}");$excel->getActiveSheet()->setCellValue("$letter[8]$i","{$data['weixin']}");$excel->getActiveSheet()->setCellValue("$letter[9]$i","{$data['wx_name']}");$excel->getActiveSheet()->setCellValue("$letter[10]$i","{$data['status_str']}");$excel->getActiveSheet()->setCellValue("$letter[11]$i","{$data['process_str']}");$excel->getActiveSheet()->setCellValue("$letter[12]$i","{$data['gift_send_str']}");}//创建Excel输入对象$write = new \PHPExcel_Writer_Excel5($excel);$filename = $this->execl_url.'/report_list_'.date("Y-m-d-H.i.s").'.xls';$write->save($filename);// 进行下一步文件压缩if($is_download_mv){$this->mv_arr[] = $filename;$this->filezip();}else{$result['code'] =200;$result['filename'] =$filename;$this->ajaxReturn($result);return true;}echo 0;
// 直接下载的代码
// header("Pragma: public");
// header("Expires: 0");
// 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="客栈信息.xls"');
// header("Content-Transfer-Encoding:binary");
// $write->save('php://output');}
导出后直接下载代码
//创建Excel输入对象$write = new \PHPExcel_Writer_Excel5($excel);$write->save($filename);// 直接下载的代码header("Pragma: public");header("Expires: 0");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="客栈信息.xls"');header("Content-Transfer-Encoding:binary");$write->save('php://output');
这篇关于ThinkPHP3.2 + PHPExcel 进行execl文件导出操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!