PHPExcel与ajax结合进行文件下载
Posted 雨中慢步
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHPExcel与ajax结合进行文件下载相关的知识,希望对你有一定的参考价值。
本实例是基于thinkphp3.2框架,ajax-post异步请求,因为是异步请求,所以请求成功之后页面不会刷新亦不会跳转,所以当时为 了将生成的excel数据导出到本地,后台采用了这种方式生成excel表格: namespace Home/Controller; use Think/Controller; class dataDeal extends Controller{ public function load_excel(){ $data = I('post.'); if (!$data){ $this->apiOut(119); } vendor('PHPExcel'); $phpExcel = new \PHPExcel(); vendor('PHPExcel_Style_Alignment'); $i = 3; $interval = null; $first = reset($data); $user_data = $first['data']; $first_key = key($data); /*start---判断是日、周、月转化率*/ if (strtotime($first_key)){ $interval = '日'; }elseif (strtotime(explode('至',$first_key)[1])-strtotime(explode('至',$first_key)[0]) == 518400){ $interval = '周'; }else{ $interval = '月'; } /*-----end-------*/ //表头部分 $phpExcel->getActiveSheet()->getColumnDimension('A1')->setAutoSize(true); $phpExcel->getActiveSheet()->getColumnDimension('A2')->setAutoSize(true); //合并单元格 $phpExcel->getActiveSheet()->mergeCells('A1:A2'); $phpExcel->getActiveSheet()->setCellValue('A1','时间'); //垂直居中 $phpExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //水平居中 $phpExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getColumnDimension('B1')->setAutoSize(true); $phpExcel->getActiveSheet()->getColumnDimension('B2')->setAutoSize(true); $phpExcel->getActiveSheet()->mergeCells('B1:B2'); $phpExcel->getActiveSheet()->setCellValue('B1','注册用户'); $phpExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $phpExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //统计表头中用户留存含多少列 $num = (count($user_data)-3); /*start---超过26列(字母序)A-ZAA-AZBA-BZ.....计算公式----*/ $currentColumn = 'C'; $row = []; for ($j = 1; $j <= $num; $j++) { $currentColumn++; $row[] = $currentColumn; } /*------end--------*/ $phpExcel->getActiveSheet()->getColumnDimension("C1")->setAutoSize(true); $phpExcel->getActiveSheet()->mergeCells("C1:{$row[$num-2]}1"); $phpExcel->getActiveSheet()->setCellValue('C1','用户留存'); //水平居中 $phpExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //垂直居中 $phpExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $phpExcel->getActiveSheet()->getColumnDimension("C2")->setAutoSize(true); $phpExcel->getActiveSheet()->setCellValue('C2','1'."$interval".'后'); $phpExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $last_row = array_pop($row); foreach ($row as $key => $val){ $phpExcel->getActiveSheet()->getColumnDimension("{$val}2")->setAutoSize(true); $day = $key+2; $phpExcel->getActiveSheet()->setCellValue("{$val}2","$day"."$interval".'后'); $phpExcel->getActiveSheet()->getStyle("{$val}2")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle("{$val}2")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); } $phpExcel->getActiveSheet()->getColumnDimension("{$last_row}1")->setAutoSize(true); $phpExcel->getActiveSheet()->mergeCells("{$last_row}1:{$last_row}2"); $phpExcel->getActiveSheet()->setCellValue("{$last_row}1","$interval".'转化率'); $phpExcel->getActiveSheet()->getStyle("{$last_row}1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle("{$last_row}1")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); foreach ($data as $v){ //数据导入 $time = array_shift($v['data']); $phpExcel->getActiveSheet()->setCellValue("A".$i,$time); $phpExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $regCount = array_shift($v['data']); $phpExcel->getActiveSheet()->setCellValue("B".$i,$regCount); $phpExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $firstData = array_shift($v['data']); $phpExcel->getActiveSheet()->setCellValue("C".$i,$firstData); $phpExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $last_data = array_pop($v['data']); $new_arr = array_combine($row,$v['data']); foreach ($new_arr as $k=>$value){ $phpExcel->getActiveSheet()->setCellValue($k.$i,$value); $phpExcel->getActiveSheet()->getStyle($k.$i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle($k.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); } $phpExcel->getActiveSheet()->setCellValue($last_row.$i,$last_data); $phpExcel->getActiveSheet()->getStyle($last_row.$i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpExcel->getActiveSheet()->getStyle($last_row.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $i++; } vendor('PHPExcel.IOFactory'); $phpExcel->createSheet(); $WriterObj = \PHPExcel_IOFactory::createWriter($phpExcel,'Excel5'); ob_end_clean(); //本地保存路径 $filename = md5(uniqid(microtime())).'.xls'; header("Content-type:application/vnd.ms-excel;charset=gbk"); header("Content-Disposition:filename=$filename"); header('Cache-Control: max-age=0'); //返回url提供页面下载 $apiData['url'] = $this->saveExcelToLocalFile($WriterObj,$filename); //返回json数据 $this->apiOut(0,$apiData); } function saveExcelToLocalFile($objWriter,$filename){ //如果是线上环境,注意操作目录的权限 $filePath = APP_PATH.'Runtime/Data/'.$filename; $objWriter->save($filePath); return $filePath; } } 前端下载的实现(js代码): <script> $(function{ $('#button').click(function{ $.ajax({ type:'post', url:"{:U(dataDeal/load_excel)}", data:excelData,//表格数据,具体根据自己页面定义获取 dataType:'json', success:function(data){ //请求成功 if(data.code == 0){ var loadUri = data.url.replace('./','/'); document.location.href =('http://example.com'+loadUri); } } }) }) }) </script>
以上是关于PHPExcel与ajax结合进行文件下载的主要内容,如果未能解决你的问题,请参考以下文章