0

我使用 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、描述、容器、值任何想法?

4

1 回答 1

2

只需在循环之前添加带有标题的附加行,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');
于 2016-01-13T07:33:09.303 回答