大佬教程收集整理的这篇文章主要介绍了在 Laravel 8 中导出到 excel 时添加列名,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有此代码以 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/
以上是大佬教程为你收集整理的在 Laravel 8 中导出到 excel 时添加列名全部内容,希望文章能够帮你解决在 Laravel 8 中导出到 excel 时添加列名所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。