问题描述
我有此代码以 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');
}
解决方法
实现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/