Prudential Developer
Prudential Developer

Reputation: 157

How to generate heading for columns?

i generate excel using Larvel-Excel this is my function

`\Excel::create('JOBS', function($excel) {

    $excel->sheet('2015', function($sheet) {
        $jobs = \App\Job::all();
        foreach($jobs as $row){
            $data=array($row->id,$row->description,$row->vessel,$row->invoice_value);
            $sheet->fromArray(array($data),null,'A1',false,false);
        }
 });})->download('csv');

im getting out put like this correct but i want to set first row as column heading ID, Description, vessel, Value any idea??

Upvotes: 0

Views: 1840

Answers (1)

Bogdan
Bogdan

Reputation: 44556

Just add the additional row with the headings before the foreach loop, the same way you add each data row:

\Excel::create('JOBS', function ($excel) {
    $excel->sheet('2015', function ($sheet) {
        $jobs = \App\Job::all();

        // Add heading row
        $data = array('ID', 'Description', 'Vessel', 'Invoice Value');
        $sheet->fromArray(array($data), null, 'A1', false, false);

        // Add data rows
        foreach ($jobs as $row) {
            $data = array($row->id, $row->description, $row->vessel, $row->invoice_value);
            $sheet->fromArray(array($data), null, 'A1', false, false);
        }
    });
})->download('csv');

Upvotes: 2

Related Questions