Laravel Excel Maatwebsite循环遍历每行的可选列

时间:2017-05-24 16:04:26

标签: php arrays laravel maatwebsite-excel

我的Excel导出工作率为90%。第一批专栏是直截了当的,取自我的收藏。最后两个字段来自相关模型,我对测试名称和测试分数感兴趣。 对于每一行(运动员),有多个测试。我需要运动员做的每一项测试都是一个专栏。

我得到了它,但测试分数没有正确对齐。如果运动员1进行了测试A,B和C,但运动员2仅进行了测试B和C,则将运动员2的B分数置于A测试栏下。见附图。 147.000的黄色突出显示都应该向右移动,所以最后两行,3.6和3.8都是20米冲刺测试的得分。 请帮助分数对齐。

enter image description here

控制器:

    if(count($tests)){
        $insert_results = array();
        foreach($tests as $key => $t) {            
            $insert_results[$key]['institution'] = $t->section->institution->name;
            $insert_results[$key]['sport'] = $t->section->sport->name;
            $insert_results[$key]['sportlevel'] = $t->section->sportlevel->long_name;
            $insert_results[$key]['testdate'] = $t->test_date->format('Y-m-d');
            $insert_results[$key]['name'] = $t->athlete->name;
            $insert_results[$key]['surname'] = $t->athlete->surname;
            $insert_results[$key]['ethnicity'] = $t->athlete->race;
            $insert_results[$key]['gender'] = $t->athlete->gender;
            $insert_results[$key]['age'] = $t->age;
            $insert_results[$key]['weight'] = $t->weight;
            $insert_results[$key]['height'] = $t->height;
            foreach($t->results as $r){
                $insert_results[$key]['test'.$r->battery->name] = $r->score;
            }
        }
        $filename = $section_obj->institution->name . '_' . $section_obj->sport->name . '_' . $section_obj->sportlevel->name . '_' . $date;
        $sheetname = $section_obj->sport->name . '_' . $section_obj->sportlevel->name;

        Excel::create($filename, function($excel) use($insert_results, $sheetname) {

            $excel->sheet($sheetname, function($sheet) use($insert_results) {
                $sheet->fromArray($insert_results);
                                // Auto filter for entire sheet
                $sheet->setAutoFilter();
                // Freeze first row
                $sheet->freezeFirstRow();

            });

        })->export('xlsx');
    }else{
        return redirect()->back()->with(['fail' => 'No results found for test day '. $date]);
    }

0 个答案:

没有答案