PHP code example of zuko / flex2cell

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

    

zuko / flex2cell example snippets


use Zuko\Flex2Cell\ExcelExporter;


$data = // your data here...

ExcelExporter::export($data, 'export.xlsx', [
    'headers' => ['#', 'Name', 'Product Group', 'Owner Name', 'Business Type', 'District', 'Commune'],
    'mapping' => [
        'id' => '#',
        'name' => 'Name',
        'product_group' => 'Product Group',
        'owner_name' => 'Owner Name',
        'owner_business_type' => 'Business Type',
        'district.name' => 'District',
        'village.name' => 'Commune',
    ],
    'formatters' => [
        'owner_business_type' => new CustomFormatter(),
        'district.name' => 'FullyQualifiedFormatterClassName',
    ],
    'columnMergeRules' => [
        ['start' => 'district.name', 'end' => 'village.name', 'label' => 'Address', 'shiftDown' => true]
    ],
    'rowMergeRules' => [
        'product_group', 'owner_name', 'owner_business_type'
    ]
]);

use Zuko\Flex2Cell\ExcelExporter;

// custom formatter class
class BusinessTypeFormatter implements Zuko\Flex2Cell\Contracts\FormatterInterface
{
    public function formatValue($value)
    {
        return ucwords(str_replace('_', ' ', $value));
    }
}


$data = // your data here...

ExcelExporter::make()
             ->setData($data)
             ->setHeaders(['ID', 'Name', 'Product Area', 'Nursery Area', 'Category'])
             ->setMapping([
                 'id' => 'ID',
                 'name' => 'Name',
                 'product_area' => 'Product Area',
                 'nursery_area' => 'Nursery Area',
                 'category.name' => 'Category'
             ])
             ->setFormatters([
                 'category.name' => fn($value) => ucfirst($value),
                 'owner_business_type' => new CustomFormatter(),
                 
             ])
             ->setColumnMergeRules([
                 ['start' => 'C', 'end' => 'D', 'label' => 'AREA']
             ])
             ->setRowMergeRules([
                 'E' => ['field' => 'category.name']
             ])
             ->export('export.xlsx');