如何为列生成标题?

How to generate heading for columns?

我使用 Larvel-Excel 生成 excel 这是我的函数

`\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');

我这样正确地输出,但我想将第一行设置为列标题 ID、描述、容器、值 有什么想法吗??

只需在 foreach 循环之前添加带有标题的附加行,方法与添加每个数据行的方式相同:

\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');