(一)环境配置
- 基础环境
- 系统环境: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)
(二)安装ThinkPHP6.0并配置
(1)安装ThinkPHP6.0
composer create-project topthink/think tp2excel
(2)安装Excel插件phpspreadsheet
composer require phpoffice/phpspreadsheet
(3)配置站点

(4)配置Nginx重写

(5)启动,查看首页

(三)配置数据库与数据库设计
(1)数据库配置

(2)数据库建表(略)
(四)查询SQL数据并导出
(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)导出结果

网友评论