通过phpexcel插件导出报表功能实现

先到官网下载 https://github.com/PHPOffice/PHPExcel/

封装导出函数:

//封装导出excel函数
function my_export($expTitle,$expCellName,$expTableData){
      $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
      $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
      $cellNum = count($expCellName);
      $dataNum = count($expTableData);

      include_once("../extend/phpexcel/PHPExcel.php");
      $objPHPExcel = new \PHPExcel();
      $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->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
      for($i=0;$i<$cellNum;$i++){
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
      }
      // Miscellaneous glyphs, UTF-8
      for($i=0;$i<$dataNum;$i++){
          for($j=0;$j<$cellNum;$j++){
              $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
          }
      }
      header('pragma:public');
      header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
      header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
      $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      $objWriter->save('php://output');
      exit;
  }

--------------------------------------------------------------------------------------------------------------------------
调用demo
public function export()
        {
                $xlsName  = "用户列表";
              $xlsCell  = array(
                  array('name','姓名'),
                  array('age','年龄'),
        );
              $lists = [
                      ['name' => '李白', 'age' => 10],
                      ['name' => '张三', 'age' => 20],
                      ['name' => '李四', 'age' => 30]
              ];
        foreach ($lists as $key => $val) {
          $lists[$key]['name'] = $val['name'];
          $lists[$key]['age'] = $val['age'];
        }

        ob_clean();
        $rs = my_export($xlsName,$xlsCell,$lists);
        echo '<script>成功导出数据</script>';exit;

        }

 



效果图如下:
 

转载于:https://www.cnblogs.com/oujianjun/p/9832325.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值