栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Laravel Maatwebsite/excel(3.1) 导出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Laravel Maatwebsite/excel(3.1) 导出

官网:https://docs.laravel-excel.com/3.1/getting-started/

安装
composer require maatwebsite/excel
导出 一、导出单个Sheet

1、service封装

data          = $data;
        $this->headings      = $headings;
        $this->title         = $title;
        $this->columnFormats = $columnFormats;
        return MaatwebsiteExcelFacadesExcel::download($this, $filename);
    }

    
    public function array() : array {
        return $this->data ? : [];
    }

    
    public function title() : string {
        return $this->title ? : '';
    }

    
    public function headings() : array {
        return $this->headings ? : [];
    }

    
    public function columnFormats() : array {
        return $this->columnFormats ? : [];
    }
}

2、使用案例

 '你这瓜保熟吗?',
                'field2' => '柚子味西瓜',
                'field3' => '柚子味香水',
                'field4' => '18000000000',
                'field5' => '12'
            ],
            [
                'field1' => '你这瓜保熟吗?',
                'field2' => '柚子味西瓜',
                'field3' => '柚子味香水',
                'field4' => '18000000000',
                'field5' => '12'
            ]
        ];
        // 标题
        $headings = ['字段1', '字段2', '字段3', '字段4', '字段5'];
        // 字段自定义格式
        $columnFormats = [
            'D' => NumberFormat::FORMAT_TEXT
        ];
        return (new ExcelExport())->download($result, $headings, 'WorkSheet', 'Demo.xlsx', $columnFormats);
    }
}

二、导出多个Sheet

1、services封装

data          = $data;
        $this->headings      = $headings;
        $this->title         = $title;
        $this->columnFormats = $columnFormats;
    }

    
    public function array() : array {
        return $this->data ? : [];
    }

    
    public function headings() : array {
        return $this->headings ? : [];
    }

    
    public function title() : string {
        return $this->title ? : '';
    }

    
    public function columnFormats() : array {
        return $this->columnFormats ? : [];
    }
}
sheets = $sheets;
        return MaatwebsiteExcelFacadesExcel::download($this, $filename);
    }

    
    public function sheets() : array {
        return $this->sheets ? : [];
    }
}



2、使用案例

 'sheet1',
                'data'          => [
                    [
                        'field1' => '你这瓜保熟吗?',
                        'field2' => '柚子味西瓜',
                        'field3' => '柚子味香水',
                        'field4' => '18000000000',
                        'field5' => '12.00'
                    ],
                    [
                        'field1' => '你这瓜保熟吗?',
                        'field2' => '柚子味西瓜',
                        'field3' => '柚子味香水',
                        'field4' => '18000000000',
                        'field5' => '12.00'
                    ]
                ],
                'columnFormats' => [
                    'D' => NumberFormat::FORMAT_TEXT,
                    'E' => NumberFormat::FORMAT_TEXT,
                ]
            ],
            [
                'sheet'         => 'sheet2',
                'data'          => [[
                    'field1' => '你这瓜保熟吗?',
                    'field2' => '柚子味西瓜',
                    'field3' => '柚子味香水',
                    'field4' => '18000000000',
                    'field5' => '12.00'
                ],
                    [
                        'field1' => '你这瓜保熟吗?',
                        'field2' => '柚子味西瓜',
                        'field3' => '柚子味香水',
                        'field4' => '18000000000',
                        'field5' => '12.00'
                    ]
                ],
                'columnFormats' => [
                    'D' => NumberFormat::FORMAT_TEXT,
                    'E' => NumberFormat::FORMAT_TEXT,
                ]
            ]
        ];
        // 标题
        $headings = ['字段1', '字段2', '字段3', '字段4', '字段5'];
        $sheet    = [];
        foreach ($sheetData as &$item) {
            $sheet[] = new Multiple($item['data'], $headings, $item['sheet'], $item['columnFormats']);
        }
        return (new MultipleSheetsExport())->download($sheet);
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号