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);
}