Laravel excel export with unnecessary rows and columns
I am a beginner in Laravel, and now I am trying to export a user list in Excel. When I use Laravel Excel Export, It gives me a file with a user list and unnecessary rows and columns.
How can I remove those unnecessary?
use Exportable;
public function collection()
{
return collect();
}
public function headings(): array
{
return ['first_name','last_name','email','phone_number'];
}
I try like this but it returns excel file with extra columns E,F,G etc.. and also unnecessary rows
this code return collect(); in your collection method currently returns an empty collection
you should replace it with the logic to retrieve the actual user data.
public function collection()
{
// return collect(); your recent code
// update to this code
// tetch user data from the database
$users = \App\Models\User::select('first_name', 'last_name', 'email', 'phone_number')->get();
// this \App\Models\User you can adjust to your actual model and path
// return the collection of users
return $users;
}
@tangtang still unnecessary rows and columns, actually I mean I don't want those extra rows and columns, I just want an Excel file with only necessary columns and rows.
so you mean you dont want this column from E to XFD column and row from your last record to row 1048576 ?
in excel file you can't directly delete this column, but you can hide it, or setup a print page area. so the column and row only show the data you want to show.
no. excel file it self don't have the feature to delete column and row, even if you manually deleted it. the column still from column A to column XFD and row still from row 1 to row 1048576
the only way is you can hide this column or row you don't want.