Search code examples
laravellaravel-excel

How to add months dynamically which is stored in database as a header in excel file while exporting using laravel?


Suppose I have items in database which is stored from an Excel file. All the items should be below the header of the months. I have also stored months from the file in the database. So, I want those months to be the header of those items and it's related records. In simple words, I want the header to be dynamic. This is what I have done.

I have tried many code scripts but nothing works. Like Laravel, Excel etc. Can anyone suggest me a good approach?

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');
  }

I need help for getting the actual result.

These months are coming from database

Here I want the header to be months dynamically


Solution

  • This is the actual code which I have used for excel file. I have solved my problem. Thanks and yeah I am posting this code, if anyone can get help from it.

    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');
    }