The content of this article is about phpExecl exporting execl form. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
1: Download the PHPExecl plug-in
Download address: https://download.csdn.net/download/rainredhezhang/10359499
2: Place it in the corresponding directory,
3: Write a general export method,
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.jb51.net') ->setLastModifiedBy('http://www.jb51.net') ->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: Write data calling method:
public function explodetradelog(){ $list = [['userid'=>1,'username'=>'user1'],['userid'=2,'username'=>'user2']];// 数据 // 数据对应的表头。这里的第一个对应$list 的key值,第一个对应execl 的表头文字 $ceilname = [ ['userid','ID'], ['username','用户名'], ]; // 调用就可以了 exportexecl($list,$ceilname,"用户信息"); }
Related recommendations:
thinkphp3. 2.3 Integrate phpExcel export data
PHPExcel correctly reads the time cells of excel tables
The above is the detailed content of phpExecl export execl table. For more information, please follow other related articles on the PHP Chinese website!