Home  >  Article  >  Backend Development  >  phpExecl export execl table

phpExecl export execl table

不言
不言Original
2018-04-19 15:02:381504browse

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].&#39;1&#39;, $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).&#39;_&#39;.date(&#39;Y-m-dHis&#39;);
    //*生成xls文件
    header(&#39;Content-Type: application/vnd.ms-excel&#39;);
    header(&#39;Content-Disposition: attachment;filename="&#39;.$filename.&#39;.xls"&#39;);
    header(&#39;Cache-Control: max-age=0&#39;);
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);
    
    $objWriter->save(&#39;php://output&#39;);

    exit;
}

4: Write data calling method:

public function explodetradelog(){
		$list = [[&#39;userid&#39;=>1,&#39;username&#39;=>&#39;user1&#39;],[&#39;userid&#39;=2,&#39;username&#39;=>&#39;user2&#39;]];// 数据


                // 数据对应的表头。这里的第一个对应$list 的key值,第一个对应execl 的表头文字
		$ceilname = [
		[&#39;userid&#39;,&#39;ID&#39;],
		[&#39;username&#39;,&#39;用户名&#39;],
		];
                // 调用就可以了
		exportexecl($list,$ceilname,"用户信息");
}

Related recommendations:

phpexcel Chinese tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn