Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

naywinhlaing's avatar

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?

0 likes
10 replies
tangtang's avatar

@naywinhlaing

you can use map function to filter the unnecessary columns.

and, can you show the export controller ?

naywinhlaing's avatar

@tangtang of course

   {
       $export = new UserExport;
       $export->store('excelFiles/user_excel.xlsx', 'excel');
       $path = asset('excelFiles/user_excel.xlsx');
       $response = response($path, 200)
           ->header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
           ->header('Content-Disposition', 'attachment; filename="user_excel.xlsx"');

       return $response;
   }
tangtang's avatar

@naywinhlaing

not the download function but show the code of this UserExport class

you can use something like this in this class

$users = User::select('name', 'email')->get(); // you can use something like this to select desired column

return $users;
naywinhlaing's avatar

@tangtang

 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

tangtang's avatar

@naywinhlaing

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;
    }
naywinhlaing's avatar

@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.

tangtang's avatar

@naywinhlaing

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.

tangtang's avatar

@naywinhlaing

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.

Please or to participate in this conversation.