我正在尝试使用“Laravel excel”将一些数据从我的模型导出到 excel 表格中,我已经做到了,但我的结果与我真正需要的相差甚远
这就是我在下载文件中得到的:
这就是我真正想要完成的:
我的控制器部分:
//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
数组由数据库中的字段名称组成,因此它基本上决定了导出中包含哪些列。这意味着我$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...
太……我的问题是:
- 如何设置主标题(例如 img 中的“标题内容”)?
- 为什么我的列标题(date_start,ts_category,ts_make,ts_model)在第一个数组元素中消失了?(你可以在第一个 img 看到我没有第一个子数组的“日期开始”和“ts_category”标题。顺便说一句,有时我根本没有第一个子数组的标题!)
- 我可以将子数组的标题设为粗体(ts_make 等)吗?如何?
- 如何为每个代理制作字幕(我打算使用子数组键显示多个代理)?
更新发布的工作代码作为答案。