【问题标题】:Adding column names when exporting to excel in Laravel 8在 Laravel 8 中导出到 excel 时添加列名
【发布时间】:2021-07-21 18:53:32
【问题描述】:

我有这段代码可以以 excel 格式下载我的表格,但是没有显示列名,因此不了解数据库的人无法读取数据。下载时有什么办法可以在excel文档中添加列名吗?

这是我的代码:

UsersExport.php

<?php

namespace App\Exports;

use App\User;
use App\Models\Models\Energy;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class UsersExport implements FromCollection
{

    public function collection()
    {
        return Energy::all();
    }       
}

能量控制器

public function export()
    {
 return Excel::download(new UsersExport, 'invoices.xlsx');

}

【问题讨论】:

    标签: php mysql laravel laravel-8 maatwebsite-excel


    【解决方案1】:

    实现WithHeadings,并将它们定义为一个数组:

    class UsersExport implements FromCollection,WithHeadings {
    
          public function headings(): array {
              return [
                  "name","mobile","email"
              ];
          }
    

    ..

    检查这个例子: https://makitweb.com/export-data-in-excel-and-csv-format-with-laravel-excel/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多