Installing ThinkPHP 6.0, Configuring the Environment, and Exporting Data to Excel with PhpSpreadsheet

This tutorial walks through setting up a Windows‑based PHP development environment, installing ThinkPHP 6.0 and the PhpSpreadsheet library, configuring Nginx and the database, and providing complete code to query MySQL data and export it as an Excel file.

php Courses
php Courses
php Courses
Installing ThinkPHP 6.0, Configuring the Environment, and Exporting Data to Excel with PhpSpreadsheet

First, prepare the development environment on Windows 10 x64 with PHP 7.4.4, MySQL 8.0.19, Nginx 1.16.1, phpEnv 7.1.5, Navicat Premium, and Composer for dependency management.

Install ThinkPHP 6.0 using Composer: composer create-project topthink/think tp2excel Then add the Excel handling library PhpSpreadsheet: composer require phpoffice/phpspreadsheet Configure the ThinkPHP site and set up Nginx rewrite rules as shown in the original screenshots.

Configure the MySQL database (e.g., using Navicat) and create the necessary tables for storing IP address ranges.

Import the Spread.php helper, query data from the b_demo table, and define the Excel export parameters such as file name, column headers, column widths, and row heights.

<?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());
        $Excel['cellName'] = ['A','B','C','D'];
        $Excel['H'] = ['A'=>12,'B'=>22,'C'=>28,'D'=>38]; // column widths
        $Excel['V'] = ['1'=>40,'2'=>26]; // row heights
        $Excel['sheetTitle'] = $fileName;
        $Excel['xlsCell'] = [
            ['id','编号'],
            ['start','开始IP'],
            ['end','结束IP'],
            ['disp','地区']
        ];
        Spread::excelPut($Excel, $expTableData);
    }
}

Running the excel method generates an Excel file containing the queried IP address data, which can be downloaded via the link on the index page.

The article also includes screenshots of the configuration steps, Nginx rewrite settings, database design, and the final exported Excel file.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPNginxexcel-exportThinkPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.