ThinkPHP5 整合PHPExcel導(dǎo)出

1、安裝PHPExcel到thinkphp5
直接使用composer require phpoffice/phpexcel 命令即可自動(dòng)下載到框架目錄vendor下,如果不會(huì)使用可直接下載好PHPExcel插件文件夾手動(dòng)放入vendor\phpoffice\下
2、使用
1)這里我寫了一個(gè)擴(kuò)展類到框架擴(kuò)展文件夾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"];

/**
 * 架構(gòu)方法 設(shè)置參數(shù)
 */
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');
}
/**
 * 設(shè)置標(biāo)題欄
 */
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');

}
/**
 * 設(shè)置內(nèi)容顯示
 */
private function setContents()
{
    
    $r = 2;
    foreach ($this->rows as $k => $v) {
        $this->setRowsContent($r,$v);
        $r++;
    }
}
/**
 * 設(shè)置每行數(shù)據(jù)
 */
private function setRowsContent($rownum,$data)
{
    $styleThinBlackBorderOutline = array(
        'borders' => array (
            'outline' => array (
                'style' => PHPExcel_Style_Border::BORDER_THIN,   //設(shè)置border樣式
                //'style' => PHPExcel_Style_Border::BORDER_THICK,  另一種樣式
                'color' => array ('argb' => 'FF000000'),          //設(shè)置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)用法
控制器文件引入擴(kuò)展use \org\Excel;
① 獲取數(shù)據(jù)庫數(shù)據(jù)
② 實(shí)例化下載類
③ 傳入文件名、結(jié)果集、菜單數(shù)組
通過以上三步即可開始下載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 = "檢測(cè)類型";
    $menus = ["ID","類型名稱","添加時(shí)間","是否刪除"];
      // print_r($rows) ;
        $ex->downExcel($title,$rows,$menus);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 是什么 如果你知道yum、apt-get、npm、bower等命令中的一種或者多種,那么,你也能很快知道compo...
    旱魃一樣閱讀 3,365評(píng)論 0 9
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評(píng)論 19 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,446評(píng)論 4 61
  • 前言 終于有那么點(diǎn)時(shí)間能將Laravel 5的一些好的實(shí)踐總結(jié)出來,希望為普及Laravel和新的PHP編程思想出...
    該葉無法找到閱讀 6,347評(píng)論 0 47
  • Laravel框架筆記 一、 composer的安裝: 1.Composer是什么?是 PHP 用來管理依賴(de...
    李景磊閱讀 1,128評(píng)論 0 4

友情鏈接更多精彩內(nèi)容