PHP code example of lab36 / excel-report

1. Go to this page and download the library: Download lab36/excel-report library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lab36 / excel-report example snippets



namespace App\ExcelReport;

use Lab36\ExcelReport\ExcelReport;

class ProjectDetailsExcelReport extends ExcelReport
{
    public function columnMappings(): array
    {
        return [
            'project' => 'Project name',
            'start_date' => 'Project date',
            'cost' => 'Cost',
            'type' => 'Type',
            'state'=> 'State',
            'client_name'=>'Client name',
            'client_address'=>'Client address',
        ];
    }
}




namespace App\Http\Controllers;

use App\ExcelReport\ProjectDetailsExcelReport;
use App\Http\Controllers\Controller;
use Faker\Factory;


class ReportController extends Controller
{
    public function exportProjects()
    {
        $faker = Factory::create();

         $project_data = collect([
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(2500, 3000),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(1000, 2000),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(4200, 4500),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
        ]);

     
        return ProjectDetailsExcelReport::fromCollection(
             'Project details',
             $project_data
        )->download();
    }
}

 $employee_data = [
        [
            'name' => $faker->name,
            'start_date' => $faker->date('Y-m-d'),
            'employee_cost' => $faker->numberBetween(100, 200),
        ],
        [
            'name' => $faker->name,
            'start_date' => $faker->date('Y-m-d'),
            'employee_cost' => $faker->numberBetween(100, 200),
        ],
    ];


public function columnMappings(): array
    {
        return [
            'name' => __('Name'),
            'start_date' => 'Start date',
            'employee_cost' => 'Employee cost',
        ];
    }


    public function columnAlignment(): array
    {
        return [
            'start_date' => 'CENTER',
        ];
    }

    public function columnFormats(): array
    {
        return [
            'start_date' => 'DATE_YYYY-MM-DD',
            'employee_cost' => 'NUMERIC_FORMATTED',
        ];
    }

    public function columnWidth(): array
    {
        return [
            'name' => '40',
        ];
    }

 return ProjectDetailsExcelReport::fromCollection(
             'Project details',
             $project_data
        )
        ->setTitleRowNo(2)
        ->setFilterRowNo(5)
        ->setHeaderRowNo(6)
        ->download();

$project_data = Project::where('name','=','John')
                        ->where('start_date', '=', '2018-01-01')
                        ->get();

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->setFilters([
                                            'name' => 'John',
                                            'start_date' => '2018-01-01',
                                            ])
                                ->download();

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->download('Project details '.Carbon::now()->toDateString());

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->store('Project details '.Carbon::now()->toDateString(), public_path());

public function overrideCell(Worksheet $active_sheet, int $current_row_no, int $column_no, $value, array $row_data, string $column_name)
{
    if ($column_name == 'order_type' && is_numeric($value)) {
        $active_sheet->setCellValue(Coordinate::stringFromColumnIndex($column_no).$current_row_no,
                                    config('enums.order_type')[$value]);
    }
}
bash
php artisan excel:report ProjectDetailsExcelReport
bash
php artisan vendor:publish --provider="Lab36\ExcelReport\ExcelReportServiceProvider"
bash
php artisan excel:report ClassName