thinkphp5+phpspreadsheet实现单表单sheet导出excel

本文涉及接口类和抽象类,看似没关系,实际就是没什么关系,直接使用即可,下面是代码:

需要掌握的技能:使用composer引入phpspreadsheet代码

实现一个Base基本抽象类

1
2
3
4
5
6
7
8
9
10
11
12
<?php
declare (strict_types = 1);
namespace app\index\stract;

/**
 * 实现一个Base基本抽象类
 */
abstract class QueryBase
{
    public $public_url = 'http://www.xxxxxxxxx.cn';

}

继承公共抽象类实现导出抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
declare (strict_types = 1);
namespace app\index\stract;

/**
 * 导入composer引入的phpspreadsheet类
 */
use PhpOffice\PhpSpreadsheet\Spreadsheet;

/**
 * 继承公共抽象类实现导出抽象类
 */
abstract class Export extends QueryBase
{
    public $spreadsheet;

    abstract function loadExportClass(): object;

}

实现一个excel导出接口类,写出继承类需要实现的几个方法

1
2
3
4
5
6
7
8
9
10
11
12
<?php
declare (strict_types = 1);
namespace app\index\face;

/**
 * 实现一个excel导出接口类,写出继承类需要实现的几个方法
 */
interface Export
{
    function businessExport(): bool;

}

实现最终的导出类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare (strict_types = 1);
namespace app\index\controller;
use app\index\face\Export as ExportInterface;
use app\index\model\Business;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use app\index\stract\Export as ExportStract;

class Export extends ExportStract implements ExportInterface
{
    //定义文件名称
    public $file_name = '';

    //构造方法
    public function __construct()
    {
        self::loadExportClass();
    }

    //实现抽象类的方法
    public function loadExportClass(): object
    {
        $this->spreadsheet = new Spreadsheet();
        return $this->spreadsheet;
    }

    //导出实际方法
    public function businessExport(): bool
    {
        $sheet = $this->spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'id');
        $sheet->setCellValue('B1', '名称');
        $sheet->setCellValue('C1', '年龄');
        $sheet->getColumnDimension('A')->setWidth(10);
        $sheet->getColumnDimension('B')->setWidth(15);
        $sheet->getColumnDimension('C')->setWidth(50);
        $data = Business::all();
        $count = count($data);  //计算有多少条数据
        //循环处理每条数据结果
        for ($i = 2; $i <= $count+1; $i++) {
            $sheet->setCellValue('A' . $i, $data[$i-2]['id']);
            $sheet->setCellValue('B' . $i, $data[$i-2]['name']);
            $sheet->setCellValue('C' . $i, $data[$i-2]['age']);
        }
        $this->file_name = '表名称';
        return true;
    }

    //析构方法,所有方法结束之后都执行
    public function __destruct()
    {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$this->file_name.'.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = new Xlsx($this->spreadsheet);
        $writer->save('php://output');
        //删除清空
        $this->spreadsheet->disconnectWorksheets();
        unset($this->spreadsheet);
        exit;
    }

}

最后直接运行businessExport方法即可导出数据

本文为 今天也想见到你 博客文章,转载无需和我联系,但请注明来自 今天也想见到你 博客 0925.wang
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇