PHP code example of fastbolt / excel-writer

1. Go to this page and download the library: Download fastbolt/excel-writer 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/ */

    

fastbolt / excel-writer example snippets


$ composer 

    $columns = [
            new ColumnSetting('Name', ColumnSetting::FORMAT_STRING, 'getName'),
            new ColumnSetting('ID', ColumnSetting::FORMAT_INTEGER, 'getId'),
    ];

    $generator = new ExcelGenerator();
    $file = $generator
                ->setContent($data)
                ->setColumns($columns)
                ->generateSpreadsheet('../var/temp/excelwriter');

    $columns = [
            new ColumnSetting('Loginname', ColumnSetting::FORMAT_STRING, static function($user) {
               return $user->getLoginname();
            })
        ];

    $headerStyle = [
        'fill' => [
            'fillType' => Fill::FILL_SOLID,
            'color' => array('rgb' => 'FF9933')
        ]
    ];
    $dataRowStyle = [
        'fill' => array(
            'fillType' => Fill::FILL_SOLID,
            'color' => array('rgb' => '66FF66')
        )
    ];
    
    $style = new TableStyle();
    $style
        ->setHeaderRowHeight(2)
        ->setHeaderStyle($headerStyle)
        ->setDataRowStyle($dataRowStyle);


    $file = $generator
        ->setContent($data)
        ->setColumns($columns)
        ->setStyle($style)
        ->generateSpreadsheet('../var/temp/filename');

     $headerStyle = [
            'fill' => [
                'fillType' => Fill::FILL_SOLID,
                'color' => array('rgb' => 'FF6622')
            ]
    ];
    $dataStyle = [
            'fill' => array(
                'fillType' => Fill::FILL_SOLID,
                'color' => array('rgb' => '22DD33')
            )
    ];
   
   //using the constructor
   $column = new ColumnSetting("By Constructor", ColumnSetting::FORMAT_STRING, '', 0, $headerStyle, $dataStyle);
   
    //using the setters 
    $column = new ColumnSetting("Styled Column", ColumnSetting::FORMAT_STRING);
    $column->setHeaderStyle($headerStyle)
           ->setDataStyle($dataStyle);

    $generator->setAutoFilterRange("A1:R14");

$generator->mergeCells(['A1:B4'])
          ->mergeCells(['P4:T4', 'S1:T1'])

    $data = [
        [
            $users[0],            //instance of a user entity
            'Italy',
            new DateTime('NOW')
        ],
        [
            $users[1],           //instance of a user entity
            'France',
            new DateTime('NOW')
        ]
    ];

    //define columns matching the order of the data
    $columns = [
        new ColumnSetting('Login', ColumnSetting::FORMAT_INTEGER, static function($user) {
            return $user->getLoginname();
        }),
        new ColumnSetting('Country', ColumnSetting::FORMAT_STRING),
        new ColumnSetting('Date', ColumnSetting::FORMAT_DATE)
    ];

    //generate
    $generator = new ExcelGenerator();

    $file = $generator
        ->setContent($data)
        ->setColumns($columns)
        ->generateSpreadsheet('../var/temp/filename');

    $repo = $this->getDoctrine()->getRepository(User::class);
    $users = $repo->findBy(['client' => 5]);

    //define columns matching the order of the data
    $columns = [
        new ColumnSetting('Login', ColumnSetting::FORMAT_INTEGER, 'getLoginName'),
        new ColumnSetting('Country', ColumnSetting::FORMAT_STRING, static function($user) {
            return $user->getCountry()->getName();
        }),
        new ColumnSetting('Created', ColumnSetting::FORMAT_DATE, 'getCreated')
    ];

    //generate
    $generator = new ExcelGenerator();

    $file = $generator
        ->setContent($users)
        ->setColumns($columns)
        ->generateSpreadsheet('../var/temp/filename');

    foreach ($userGroups as $userGroup) {
        $excelGenerator
                    ->setTitle($userGroup->getName())
                    ->setContent($userGroup)
                    ->setColumns($columns)
                    ->nextWorksheet();
    }
    $excelGenerator->generateSpreadsheet('../var/temp/filename');

    $repo  = $this->getDoctrine()->getRepository(User::class);
    $users = $repo->findBy(['client' => 5]);

    //define columns
    $columns = [
        new ColumnSetting('Login', ColumnSetting::FORMAT_INTEGER, 'getLoginName'),
        new ColumnSetting('Country', ColumnSetting::FORMAT_STRING, static function($user) {
            return $user->getCountry()->getName();
        }),
        new ColumnSetting('Created', ColumnSetting::FORMAT_DATE, 'getCreated'),
        new ColumnSetting('Weight', ColumnSetting::FORMAT_FLOAT, 'getWeight', 2)
    ];
    
    //set style
    $headerStyle = [
        'fill' => [
            'fillType' => Fill::FILL_SOLID,
            'color' => array('rgb' => 'FF9933')
        ]
    ];
    $dataRowStyle = [
        'fill' => array(
            'fillType' => Fill::FILL_SOLID,
            'color' => array('rgb' => '66FF66')
        )
    ];
    
    $style = new TableStyle();
    $style
        ->setHeaderRowHeight(2)
        ->setHeaderStyle($headerStyle)
        ->setDataRowStyle($dataRowStyle);

    //generate
    $generator = new ExcelGenerator();

    $file = $generator
        ->setContent($users)
        ->setColumns($columns)
        ->setStyle($style)
        ->generateSpreadsheet('../var/temp/filename');

    //download
    $response = new BinaryFileResponse($file->getPathname());
    $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
    return $response;