PHP code example of esmaeil / excelmapper

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

    

esmaeil / excelmapper example snippets


use ExcelMapper\DataProcessor\ExcelDataProcessor;
use ExcelMapper\Readers\ExcelReader;
use ExcelMapper\Parsers\DefaultParser;

// Define custom column mapping
$mapping = [
    'A' => 'first_name',
    'B' => 'last_name',
    'C' => ['phone_number', DefaultParser::class],
];

// Read Excel file
$reader = new ExcelReader();
$sheetData = $reader->read('path_to_file.xlsx');

// Process the data
$processor = new ExcelDataProcessor();
$processor->process($sheetData, $mapping, function($mappedData) {
    // Handle the mapped data (e.g., save to database)
    print_r($mappedData);
});

use ExcelMapper\Interfaces\ColumnParserInterface;

class UppercaseParser implements ColumnParserInterface
{
    public function parse(mixed $value): mixed
    {
        return strtoupper($value);
    }
}

$mapping = [
    ['first_name', UppercaseParser::class],
    ['last_name', UppercaseParser::class],
];