Home  >  Article  >  PHP Framework  >  [Share Case] ​​ThinkPHP6.0 Export to Excel

[Share Case] ​​ThinkPHP6.0 Export to Excel

藏色散人
藏色散人forward
2020-12-07 14:14:123961browse

Below, the thinkphp framework tutorial column will share with you a ThinkPHP6.0 export Excel case. I hope it will be helpful to friends in need!

[Share Case] ​​ThinkPHP6.0 Export to Excel

(1) Environment configuration

  • Basic environment
    • System environment: Windows10 x64
    • PHP integrated environment: phpEnv7.1.5 (https://www.phpenv.cn/)
      • PHP7.4.4
      • MySQL8.0.19
      • Nginx1.16.1
    • Database management tool: Navicat Premium 15.0.11
    • PHP dependency management tool: Composer (https://getcomposer.org/Composer-Setup.exe)

(2) Install ThinkPHP6.0 and configure

(1) Install ThinkPHP6.0

composer create-project topthink/think tp2excel

(2) ) Install the Excel plug-in phpspreadsheet

composer require phpoffice/phpspreadsheet

(3) Configure the site

[Share Case] ​​ThinkPHP6.0 Export to Excel

(4) Configure Nginx rewrite

[Share Case] ​​ThinkPHP6.0 Export to Excel
(5) Start and view the homepage

[Share Case] ​​ThinkPHP6.0 Export to Excel

(3) Configuration database and database design

(1) Database configuration

[Share Case] ​​ThinkPHP6.0 Export to Excel

(2) Database table creation (omitted)

(4) Query SQL data and export

(1 ) Introduce the Spread.php plug-in

[Share Case] ​​ThinkPHP6.0 Export to Excel
(2) Query data and export

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use Tools\Spread;

class Index extends BaseController
{
    public function index()
    {
        return &#39;<html><a href="/index/excel.html?limit=2000">导出Excel</a><html>&#39;;
    }

    public function excel($limit = 10)
    {
        $expTableData = Db::table(&#39;b_demo&#39;)->limit($limit)->select();
        $fileName = "IP地址导出";
        $Excel[&#39;fileName&#39;]=$fileName.date(&#39;Y年m月d日-His&#39;,time());//or $xlsTitle
        $Excel[&#39;cellName&#39;]=[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;];
        $Excel[&#39;H&#39;] = [&#39;A&#39;=>12,&#39;B&#39;=>22,&#39;C&#39;=>28,&#39;D&#39;=>38];//横向水平宽度
        $Excel[&#39;V&#39;] = [&#39;1&#39;=>40,&#39;2&#39;=>26];//纵向垂直高度
        $Excel[&#39;sheetTitle&#39;]=$fileName;//大标题,自定义
        $Excel[&#39;xlsCell&#39;]=[
            [&#39;id&#39;,&#39;编号&#39;],
            [&#39;start&#39;,&#39;开始IP&#39;],
            [&#39;end&#39;,&#39;结束IP&#39;],
            [&#39;disp&#39;,&#39;地区&#39;]];
        Spread::excelPut($Excel,$expTableData);
    }
}

(3) Export the results

[Share Case] ​​ThinkPHP6.0 Export to Excel

The above is the detailed content of [Share Case] ​​ThinkPHP6.0 Export to Excel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete