PHP code example of samagtech / excel-lib

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

    

samagtech / excel-lib example snippets


    $writer = new \SamagTech\ExcelLib\Writer($path);

    $writer = (new \SamagTech\ExcelLib\Factory())->createWriter($path);

    use SamagTech\ExcelLib\Writer;

    $writer = new Writer($path, $filename, $ignoreFieldsFormat);

    or

    $writer = new Writer($path, ignoreFieldsFormat: $ignoreFieldsFormat);

    or

    $writer = new Writer($path);
    $writer->setFilename($filename)->setIgnoreFieldsFormat($ignoreFieldsFormat);


    $writer->setPath($path);

    $filePath = $writer->setHeader($headers)->setBody($body)->build();

    $columnDefinition = [
        'name'          => 'string',
        'age'           =>  'number',
        'perc_fat'      =>  'percentage'
    ];

    $body = [
        // Foglio 1
        'sheet_1' => [
            // Riga 1
            [
                // Dati
            ],
            // Riga 2
            [
                // Dati
            ]
        ],
        // Foglio 2
        'sheet_2' => [
            // Riga 1
            [
                // Dati
            ],
            // Riga 2
            [
                // Dati
            ]
        ]
    ];

    $filePath = $writer->setBody($body)->build(true);


    $header = [
        // Intestazione foglio 1
        [
            // Stringhe che diverranno colonne
        ],
        // Intestazione foglio 2
        [
            // Stringhe che diverranno colonne
        ]
    ]

    or

    // In questo caso non essendo diviso l'intestazione verrà duplicata per ogni foglio
    $header = [
        // Stringhe che diverranno colonne
    ]

    $reader = new \SamagTech\ExcelLib\Reader($path, $filename);

    $reader = (new \SamagTech\ExcelLib\Factory())->createReader($path, $filename);

    $reader->setPath($path)

    or

    $reader->setFilename($filename)

    $array = $reader->toArray();

    or

    $object = $reader->toObject();


    // Excel con foglio 1 e 2

    $array = $reader->toArray();

    // L'array sarà strutturato con il nome dei fogli dove gli spazi verranno modificati in underscore

    /**
     * $array = [
     *  'Foglio_1' => [
     *      [
     *          // Riga 1
     *      ],
     *      [
     *          // Riga 2
     *      ]
     *  ],
     *  'Foglio_2'   =>  [
     *      [
     *         // Riga 1
     *      ],
     *      [
     *          // Riga 2
     *      ]
     * ]
     *
     *
     * /



    $customColumnToKey = [
        'Nome'      => 'firstname',
        'Cognome'   => 'lastname',
        'Età'       =>  'age'
    ]

    $data = $reader->setColumnToKey($customColumnToKey)->toArray();

    /**
     * Es. Excel
     *
     * Nome         | Cognome   |   Età
     * Alessandro   | Marotta   |   25
     *
     * $data = [
     *      [
     *          'firstname' => 'Alessandro',
     *          'lastname' => 'Marotta',
     *          'age' => 25,
     *      ],
     *      // Altre righe
     * ]
     *
     * /



    $sheetNames = 'Foglio 1'; // or $sheetNames = ['Foglio 1', 'Foglio 2']

    $reader->setSheetNames($sheetNames)->toArray();

    $reader->setHasHeader(false)->toArray();