Ad

How To Generate Heading For Columns?

- 1 answer

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??

Ad

Answer

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');
Ad
source: stackoverflow.com
Ad