php – Laravel Excel,从模型导出,样式问题

前端之家收集整理的这篇文章主要介绍了php – Laravel Excel,从模型导出,样式问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将我的模型中的一些数据导出到带有“Laravel excel”的excel scheet,我已经完成了,但我的结果远非我真正需要的
这就是我在下载文件中得到的:

这就是我真正想要实现的目标:

我的控制器部分:

//casting export...
        Excel::create('ExcelExport',function($excel) use($filters,$agents) {

            $main_arr = array();

            foreach($agents as $value){                 
                $main_arr[] = Card::cardForUser($value,$filters)->toArray();                   
            }

            $excel->sheet('Sheetshit',function($sheet) use($main_arr) {
                //You may ask me "why are you using foreach?"
                // and my answer will be:"I don`t KNOW,because it WORKS!"

                foreach($main_arr as $one){
                    $sheet->fromArray($one);
                }

            });

        })->export('xls');

型号部分:

public static  function  cardForUser($user_id,$filters = array()){
        $query = static::UserId($user_id);//just gets 'where user id'

        foreach($filters['fields'] as $select){
            $query->addSelect($select);
        }
            return $query->get();
    }    

    public function scopeUserId($query,$user_id) {
        return $query->where('user_id','=',$user_id);

    }

$filters数组由来自DB的字段名称组成,因此它基本上决定导出中包含哪些列.这意味着我的$main_arr可能有1到5的内部字段长度.每个代理可以在DB中有很多行,或者根本没有行

设置了4个过滤器的$main_arr转储示例:

array (size=8)
  0 => 
    array (size=10)

//thats will be first agent
          0 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chrysler' (length=8)
              'ts_model' => string 'PT CRUISER' (length=10)
          1 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Opel' (length=4)
              'ts_model' => string 'Corsa' (length=5)
          2 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Dodge' (length=5)
              'ts_model' => string 'Stratus' (length=7)
          3 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Р’РђР—' (length=6)
              'ts_model' => string '2112' (length=4)
          4 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Mercedes-Benz' (length=13)
              'ts_model' => string 'E 270' (length=5)
          5 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Р’РђР—' (length=6)
              'ts_model' => string '21140 LADA SAMARA' (length=17)
          6 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'LADA' (length=4)
              'ts_model' => string '213100 LADA 4С…4' (length=16)
          7 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Р’РђР—' (length=6)
              'ts_model' => string '21110' (length=5)
          8 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Lanos' (length=5)
          9 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'VOLKSWAGEN' (length=10)
              'ts_model' => string 'PASSAT' (length=6)
//thats will be second agent
      1 => 
        array (size=10)
          0 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Mercedes' (length=8)
              'ts_model' => string 'Benz' (length=4)
          1 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Corvette' (length=8)
          2 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Corvette' (length=8)
          3 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Corvette' (length=8)
          4 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Corvette' (length=8)
          5 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Chevrolet' (length=9)
              'ts_model' => string 'Corvette' (length=8)
          6 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Mazeratti' (length=9)
              'ts_model' => string 'M4' (length=2)
          7 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Ferrari' (length=7)
              'ts_model' => string 'F4' (length=2)
          8 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Mazda' (length=5)
              'ts_model' => string '5' (length=1)
          9 => 
            array (size=4)
              'date_start' => string '06.08.2014 10:00:00' (length=19)
              'ts_category' => int 2
              'ts_make' => string 'Test' (length=4)
              'ts_model' => string 'Test' (length=4)
        etc...

Sooo …我的问题是:

>如何设置主标题(示例img中的“标题内容”)?
>为什么我的列标题(date_start,ts_category,ts_make,ts_model)会在第一个数组元素中消失? (你可以在第一个img看到我没有“date start”和第一个子数组的“ts_category”标题.顺便说一下,有时我根本没有第一个子数组的标题!)
>我可以将子数组的标题加粗(ts_make等)吗?怎么样?
>如何为每个代理制作子标题(我计划使用子数组键来显示多个代理)?

更新发布工作代码作为答案.

我不会回答你的所有问题,因为你需要自己查看文档.但是我会告诉你如何获得这样的效果

而且我认为它不仅仅能解释你做错了什么

以上excel文件代码如下:

Excel::create('ExcelExport',function ($excel) {

    $excel->sheet('Sheetname',function ($sheet) {

        // first row styling and writing content
        $sheet->mergeCells('A1:W1');
        $sheet->row(1,function ($row) {
            $row->setFontFamily('Comic Sans MS');
            $row->setFontSize(30);
        });

        $sheet->row(1,array('Some big header here'));

        // second row styling and writing content
        $sheet->row(2,function ($row) {

            // call cell manipulation methods
            $row->setFontFamily('Comic Sans MS');
            $row->setFontSize(15);
            $row->setFontWeight('bold');

        });

        $sheet->row(2,array('Something else here'));

        // getting data to display - in my case only one record
        $users = User::get()->toArray();

        // setting column names for data - you can of course set it manually
        $sheet->appendRow(array_keys($users[0])); // column names

        // getting last row number (the one we already filled and setting it to bold
        $sheet->row($sheet->getHighestRow(),function ($row) {
            $row->setFontWeight('bold');
        });

        // putting users data as next rows
        foreach ($users as $user) {
            $sheet->appendRow($user);
        }
    });

})->export('xls');
原文链接:https://www.f2er.com/laravel/136821.html

猜你在找的Laravel相关文章