美文网首页ThinkPHPthinkphp5
ThinkPHP5 整合PHPExcel导出

ThinkPHP5 整合PHPExcel导出

作者: 廷裕同学 | 来源:发表于2017-05-25 13:33 被阅读219次

    1、安装PHPExcel到thinkphp5
    直接使用composer require phpoffice/phpexcel 命令即可自动下载到框架目录vendor下,如果不会使用可直接下载好PHPExcel插件文件夹手动放入vendor\phpoffice\下
    2、使用
    1)这里我写了一个扩展类到框架扩展文件夹extend\org下 类文件如下

    <?php
    namespace org;
    use PHPExcel_IOFactory;
    use PHPExcel;
    use PHPExcel_Style_Fill;
    use PHPExcel_Style_Border;
    class Excel
    {
    private $PHPExcel;
    private $PHPSheet;
    private $PHPWriter;
    private $title;
    private $rows;
    private $menus;
    
    private $sysmenu = ["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"];
    
    /**
     * 架构方法 设置参数
     */
    public function __construct()
    {
        $this->PHPExcel  = new PHPExcel();
        $this->PHPSheet  = $this->PHPExcel->getActiveSheet();
        $this->PHPWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel,'Excel2007');
    }
    
    public function downExcel($title,$rows,$menus)
    {
        $this->title = $title;
        $this->rows  = $rows;
        $this->menus = $menus;
    
        $this->PHPSheet->setTitle($this->title);
        $this->setMenu();
        $this->setContents();
    
        $filename = $this->title.date("Y-m-d_H-i-s",time()).".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$filename.'"');
        header("Content-Transfer-Encoding: binary");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $this->PHPWriter->save('php://output');
    }
    /**
     * 设置标题栏
     */
    private function setMenu()
    {
        $max = count($this->menus);
        for ($i=0; $i < $max; $i++) { 
            $this->PHPSheet->setCellValue($this->sysmenu[$i]."1",$this->menus[$i]);
            $this->PHPSheet->getColumnDimension($this->sysmenu[$i])->setAutoSize(true);
        }
        $this->PHPSheet->getStyle('a1:'.$this->sysmenu[$max-1]."1")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $this->PHPSheet->getStyle('a1:'.$this->sysmenu[$max-1]."1")->getFill()->getStartColor()->setARGB('FF0070C0');
        $this->PHPSheet->getStyle('a1:'.$this->sysmenu[$max-1]."1")->getFont()->getColor()->setARGB('FFFFFFFF');
    
    }
    /**
     * 设置内容显示
     */
    private function setContents()
    {
        
        $r = 2;
        foreach ($this->rows as $k => $v) {
            $this->setRowsContent($r,$v);
            $r++;
        }
    }
    /**
     * 设置每行数据
     */
    private function setRowsContent($rownum,$data)
    {
        $styleThinBlackBorderOutline = array(
            'borders' => array (
                'outline' => array (
                    'style' => PHPExcel_Style_Border::BORDER_THIN,   //设置border样式
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,  另一种样式
                    'color' => array ('argb' => 'FF000000'),          //设置border颜色
                ),
            ),
        );
        for ($i=0; $i < count($data); $i++) { 
            $keyarr = array_keys($data);
            $keyvalue = $keyarr[$i];
            $this->PHPSheet->setCellValue($this->sysmenu[$i].$rownum,$data[$keyvalue]);
            $this->PHPSheet->getStyle($this->sysmenu[$i].$rownum)->applyFromArray($styleThinBlackBorderOutline);
            $this->PHPSheet->getColumnDimension($this->sysmenu[$i])->setAutoSize(true);
        }
    }
    }
    ?>
    

    2)用法
    控制器文件引入扩展use \org\Excel;
    ① 获取数据库数据
    ② 实例化下载类
    ③ 传入文件名、结果集、菜单数组
    通过以上三步即可开始下载excel文件

      public function index()
      {
             $rows = db("projects_animals")->where('isdel',0)->select();
             foreach ($rows as $k => $v) 
             {
                $rows[$k]["addtime"] = date("Y-m-d H:i:s",$v["addtime"]);
             }
            $ex = new Excel();
            $title = "检测类型";
        $menus = ["ID","类型名称","添加时间","是否删除"];
          // print_r($rows) ;
            $ex->downExcel($title,$rows,$menus);
    }

    相关文章

      网友评论

        本文标题:ThinkPHP5 整合PHPExcel导出

        本文链接:https://www.haomeiwen.com/subject/vntzxxtx.html