使用Laravel导出时,如何动态添加以Excel文件头形式存储在数据库中的月份?

时间:2019-02-13 06:50:20

标签: laravel laravel-excel

假设我数据库中的项目是从excel文件存储的。所有项目都应在月份标题下方。我还从文件中存储了几个月的时间。因此,我希望那些月份成为这些项目及其相关记录的标题。简单来说,我希望标题是动态的。这就是我所做的。

我尝试了许多代码脚本,但是没有任何效果。像laravel excel等。任何人都可以建议我一个好方法。

public function test(){
    $data = Item::where('category_id',7)->get()->toArray();
    $data2 = month::all();

    $itemsArray[] = ['Category Id','Item Name','Created At','Updated At'];

    foreach ($data as $value) {
        // dd($value);
        $itemsArray[] = array(

            'Category Id' => $value['category_id'],
            'Item Name' => $value['name'],
            'Created At' => $value['created_at'],
            'Updated At' => $value['updated_at'],
        );

    }

    // Generate and return the spreadsheet
        Excel::create('Items', function($excel) use ($itemsArray) {

            // Set the spreadsheet title, creator, and description
            $excel->setTitle('Items');

            // Build the spreadsheet, passing in the items array
            $excel->sheet('Items', function($sheet) use ($itemsArray) {

                $cellRange = 'A1:D1';
                // $spreadsheet->getActiveSheet()->getStyle('A1:D4')
                    // ->getAlignment()->setWrapText(true);
                $sheet->getStyle($cellRange)->getFont()->setBold( true );
                $sheet->getStyle($cellRange)->getFont()->setSize( '15' );
                $sheet->setBorder($cellRange, 'thick' );
                $sheet->getStyle($cellRange)->applyFromArray(array(
                       'fill' => array(
                           // 'type'  => PHPExcel_Style_Fill::FILL_SOLID,
                           'color' => array('rgb' => 'A5D9FF')
                       )
                   ));
                $sheet->fromArray($itemsArray, null, 'A1', false, false);
            });
            $excel->setCreator('Laravel')->setCompany('Dev505');
            $excel->setDescription('Items file');

        })->download('xlsx');
  }

我需要获得实际结果的帮助。谢谢你。

These months are coming from database

Here I want the header to be months dynamically

2 个答案:

答案 0 :(得分:0)

我建议使用Akhtar来安装Carbon套件 https://carbon.nesbot.com/docs/

请尝试更新以下代码。

$data = Item::where('category_id',7)->get(); // removed toArray()
$data2 = month::all();
$itemsArray[] = ['Category Id','Item Name','Created At','Updated At'];

foreach ($data as $key=>$value) {

    $itemsArray[] = array(
        'month' => Carbon::now()->addMonth($key)->format('m-Y');
        'Category Id' => $value['category_id'],
        'Item Name' => $value['name'],
        'Created At' => $value['created_at'],
        'Updated At' => $value['updated_at'],
    );

}

答案 1 :(得分:0)

这是我用于excel文件的实际代码。我已经解决了我的问题。谢谢,是的,如果有人可以从中获得帮助,我会发布此代码。

public function export(){

    $data = Category::all();

    foreach ($data as $value) {

        $value['items'] = Item::where('category_id',$value['id'])->get();

        foreach ($value['items'] as $vl) {

            $vl['record'] = Record::where('item_id',$vl['id'])->get();
        }
    }

    $data2 = month::pluck('id','month');

    foreach ($data2 as $key => $value) {

        $m[] = $key;
    }

    array_unshift($m, 'Categories'); //Insert new element at the start of array
    array_push($m, 'Total');

    $itemsArray[] = $m;

    foreach ($data as $value) {

        $itemsArray[] = array(

            $itemsArray[0][0] => $value['name'],
            // $itemsArray[0][13] => 'Total',
        );

        foreach ($value['items'] as $val) {

            $records_array = [];
            $i =  0;

            foreach ($val['record'] as $val5) {

                $recordval = $val5['value'];
                $records_array[$i] = $val5['value'];
                $i++;
            }

            $itemsArray[] = array(

                $itemsArray[0][0] => $val['name'],
                $itemsArray[0][1] => $records_array[0],
                $itemsArray[0][2] => $records_array[1],
                $itemsArray[0][3] => $records_array[2],
                $itemsArray[0][4] => $records_array[3],
                $itemsArray[0][5] => $records_array[4],
                $itemsArray[0][6] => $records_array[5],
                $itemsArray[0][7] => $records_array[6],
                $itemsArray[0][8] => $records_array[7],
                $itemsArray[0][9] => $records_array[8],
                $itemsArray[0][10] => $records_array[9],
                $itemsArray[0][11] => $records_array[10],
                $itemsArray[0][12] => $records_array[11],
                // $itemsArray[0][13] => 'Total',
            );

        }

    }

    // Generate and return the spreadsheet
    Excel::create('Items', function($excel) use ($itemsArray) {

        // Set the spreadsheet title, creator, and description
        $excel->setTitle('Items');

        // Build the spreadsheet, passing in the items array
        $excel->sheet('Items', function($sheet) use ($itemsArray) {
            $cellRange = 'A1:M1';
            $sheet->getStyle($cellRange)->getFont()->setBold( true );
            $sheet->getStyle($cellRange)->getFont()->setSize( '12' );
            $sheet->setBorder($cellRange, 'thin' );
            $sheet->getStyle($cellRange)->applyFromArray(array(
                   'fill' => array(
                       // 'type'  => PHPExcel_Style_Fill::FILL_SOLID,
                       'color' => array('rgb' => 'A5D9FF')
                   )
               ));
            $sheet->fromArray($itemsArray, null, 'A1', false, false);
        });
        $excel->setCreator('Laravel')->setCompany('Dev505');
        $excel->setDescription('Items file');

    })->download('xlsx');
}