- 基礎環境
- 系統環境:Windows10 x64
- PHP整合環境:phpEnv7.1.5(https://www.phpenv.cn/)
- PHP7.4.4
- MySQL8.0.19
- Nginx1.16.1
- 資料庫管理工具:Navicat Premium 15.0.11
- PHP依賴管理工具:Composer(https://getcomposer.org/Composer-Setup.exe)
(1)安裝ThinkPHP6.0
composer create-project topthink/think tp2excel
(2)安裝Excel外掛phpspreadsheet
composer require phpoffice/phpspreadsheet
(3)配置站點
(4)配置Nginx重寫
(5)啟動,檢視首頁
(1)資料庫配置
(2)資料庫建表(略)
(1)引入Spread.php外掛
(2)查詢資料並匯出
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Db;
use Tools\Spread;
class Index extends BaseController
{
public function index()
{
return '<html><a href="/index/excel.html?limit=2000">匯出Excel</a><html>';
}
public function excel($limit = 10)
{
$expTableData = Db::table('b_demo')->limit($limit)->select();
$fileName = "IP地址匯出";
$Excel['fileName']=$fileName.date('Y年m月d日-His',time());//or $xlsTitle
$Excel['cellName']=['A','B','C','D'];
$Excel['H'] = ['A'=>12,'B'=>22,'C'=>28,'D'=>38];//橫向水平寬度
$Excel['V'] = ['1'=>40,'2'=>26];//縱向垂直高度
$Excel['sheetTitle']=$fileName;//大標題,自定義
$Excel['xlsCell']=[
['id','編號'],
['start','開始IP'],
['end','結束IP'],
['disp','地區']];
Spread::excelPut($Excel,$expTableData);
}
}
(3)匯出結果
本作品採用《CC 協議》,轉載必須註明作者和本文連結