在 laravel 中导出 excel 或 csv 中的多维数组

Posted

技术标签:

【中文标题】在 laravel 中导出 excel 或 csv 中的多维数组【英文标题】:Export multidimensional array in excel or csv in laravel 【发布时间】:2018-11-20 07:08:42 【问题描述】:

我现在的情况真的很复杂。

在这里我必须将多维数组数据导出到 excel 中,如下图所示,但我无法做到。

请帮我将多维数组数据导出到excel中。

我有一个来自关系查询的数组,例如:

$array = [
    0 => [
        'name' => 'Devat Karetha',
    ],
    1 => [
        'name' => 'John Doe',
        'schools' => [
            0 => [
                'school_name' => 'ABC School',
                'school_address' => 'ABC Address',
            ],
            1 => [
                'school_name' => 'XYZ School',
                'school_address' => 'XYZ Address',
            ]
        ],
        'sports' => [
            0 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 2
                    'sport_type_name' => 'ABC Sport'
                ],
            ],
            1 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 3
                    'sport_type_name' => 'ABC Sport'
                ],
            ],
        ],
    ],
    2 => [
        'name' => 'Utkarsh Raval',
        'schools' => [],
        'sports' => [
            0 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 2
                    'sport_type_name' => 'DEKTS Sport'
                ],
            ],
        ],
        'centre' => [
            0 => [
                'id' => 4,
                'centre_name' => 'ABC Centre',
                'centre_mobile' => '9898989898',
            ],
            1 => [
                'id' => 5,
                'centre_name' => 'XYZ Centre',
                'centre_mobile' => '7878787878',
            ],
        ]
    ],
];

谢谢你...:)

【问题讨论】:

【参考方案1】:

我在这里给出我自己对这个问题的回答 - 可能会帮助人们理解多维数组的导出功能:

    $newArr = [];
    $arr = [];
    foreach($result as $key => $value) 
        $count = [count($value['schools']),count($value['centre']),count($value['sports'])];
        $arrMax = max($count);
        if($arrMax > 0)
            for($i=0; $i < $arrMax; $i++)
                $arrIndex = $i;
                $arr['sr_no'] = '';
                $arr['name'] = '';
                if($arrIndex == '0')
                    $arr['sr_no'] = $key + 1;
                    $arr['name'] = isset($value['name']) ? $value['name'] : '';
                
                $arr['school_name'] = isset($value['schools'][$arrIndex]['school_name']) ? $value['schools'][$arrIndex]['school_name'] : '';
                $arr['school_address'] = isset($value['schools'][$arrIndex]['school_address']) ? $value['schools'][$arrIndex]['school_address'] : '';
                $arr['sport_type_name'] = isset($value['sports'][$arrIndex]['sport_type']['sport_type_name']) ? $value['sports'][$arrIndex]['sport_type']['sport_type_name'] : '';
                $arr['centre_name'] = isset($value['centre'][$arrIndex]['centre_name']) ? $value['centre'][$arrIndex]['centre_name'] : '';
                $arr['centre_mobile'] = isset($value['centre'][$arrIndex]['centre_mobile']) ? $value['centre'][$arrIndex]['centre_mobile'] : '';
                $newArr[] = $arr;
            
         else 
            $arr['sr_no'] = $key + 1;
            $arr['name'] = isset($value['name']) ? $value['name'] : '';
            $arr['school_name'] = '';
            $arr['school_address'] = '';
            $arr['sport_type_name'] = '';
            $arr['centre_name'] = '';
            $arr['centre_mobile'] = '';
            $newArr[] = $arr;
        
    

    /**
     * @param $excel
     */
    \Excel::create('advance-report-'.date('d-m-YTH-i-s'), function($excel) use($newArr) 

        $excel->sheet('Sheet 1', function($sheet) use($newArr) 

            $sheet->fromArray($newArr);
            // Freeze first row
            $sheet->freezeFirstRow();
            // Set height for header
            $sheet->setHeight(1, 30);
            // Modify properties of header
            $sheet->cells('A1:AS1', function($cells) 
                $cells->setBackground('#345fbc');
                $cells->setFontColor('#ffffff');
                $cells->setValignment('center');
            );

            for( $intRowNumber = 1; $intRowNumber <= count($newArr) + 1; $intRowNumber++)
                laravelExcel($sheet, $intRowNumber); //helper function
            

        );

    )->export('xls');

在帮助文件中:

/**
 * @param null $sheet
 * @param null $intRowNumber
 */
function laravelExcel($sheet = null, $intRowNumber = null)

    try 
        $sheet->setSize('A' . $intRowNumber, 10, 18);
        $sheet->setSize('B' . $intRowNumber, 20, 18);
        $sheet->setSize('C' . $intRowNumber, 20, 18);
        $sheet->setSize('D' . $intRowNumber, 20, 18);
        $sheet->setSize('E' . $intRowNumber, 35, 18);
        $sheet->setSize('F' . $intRowNumber, 15, 18);
        $sheet->setSize('G' . $intRowNumber, 15, 18);
     catch (\Exception $e) 
        return $e->getMessage();
    

【讨论】:

这些结构在 laravel 导出中用在哪里?重新编辑并添加方法名称以在最新版本中使用

以上是关于在 laravel 中导出 excel 或 csv 中的多维数组的主要内容,如果未能解决你的问题,请参考以下文章

Excel 在 CSV 导出中导出数百个空列

如何使用 laravel 5.7 和 vue JS 在 mysql 中导入 excel 文件

使用 laravel api 在邮递员中导出 excel 文件

mac中导出CSV格式在excel中乱码

tp框架中导出csv文件excel能打开

在 Excel 工作表单元格中导出数据