时间:2021-07-01 10:21:17 帮助过:18人阅读
1:下载PHPExecl 插件
下载地址:https://download.csdn.net/download/rainredhezhang/10359499
2:放到对应的目录上,
3:写通用导出方法,
function exportexecl($data=[],$expCellName,$name="会员列表清单"){ date_default_timezone_set('Asia/Shanghai'); import('Vendor.Excel.PHPExcel'); //获取数据 $cellNum = count($expCellName);// 有多少列 $dataNum = count($data);//有多少行 $cellName = 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', 'AA','AB','AC','AD','AE', 'AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT', 'AU','AV','AW','AX','AY','AZ'); $objPHPExcel=new \PHPExcel(); $objPHPExcel->getProperties()->setCreator('http://www.gxlcms.com') ->setLastModifiedBy('http://www.gxlcms.com') ->setTitle('Office 2007 XLSX Document') ->setSubject('Office 2007 XLSX Document') ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office 2007 openxml php') ->setCategory('Result file'); for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]); } for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $data[$i][$expCellName[$j][0]]); } } $objPHPExcel->getActiveSheet()->setTitle($name); $objPHPExcel->setActiveSheetIndex(0); $filename=urlencode($name).'_'.date('Y-m-dHis'); //*生成xls文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }
4:写数据调用方法:
public function explodetradelog(){ $list = [['userid'=>1,'username'=>'user1'],['userid'=2,'username'=>'user2']];// 数据 // 数据对应的表头。这里的第一个对应$list 的key值,第一个对应execl 的表头文字 $ceilname = [ ['userid','ID'], ['username','用户名'], ]; // 调用就可以了 exportexecl($list,$ceilname,"用户信息"); }
相关推荐:
phpexcel中文教程
thinkphp3.2.3 整合phpExcel导出数据
PHPExcel正确读取excel表格时间单元格
以上就是phpExecl 导出execl 表格 的详细内容,更多请关注Gxl网其它相关文章!