1. Go to this page and download the library: Download shmandalf/excelentor 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/ */
shmandalf / excelentor example snippets
Shmandalf\Excelentor\Attributes\{Header, Column};
use Shmandalf\Excelentor\Parser;
use Shmandalf\Excelentor\ValidatorFactory;
// Excel like column names, can be numeric indexes instead or even skipped
#[Header(columns: ['A' => 'name', 'B' => 'email', 'C' => 'age'])]
class ApprenticeWizard
{
#[Column(rule: 'r a reason
// it can be configured in the Header attribute
// by passing the $rows argument
'Some header',
'Name',
'Here',
],
// Demo data
[
'Melissa',
'[email protected]',
45,
],
[
'Lera',
'[email protected]',
46,
],
[
'Anastasia',
'[email protected]',
47,
],
[
'Dmitry',
'[email protected]',
48,
],
];
// Cast the parsing incantation on the ancient scroll!
$parser = new Parser(ApprenticeWizard::class, new ValidatorFactory('ru'));
// Scry for cursed runes and magical anomalies
$errors = $parser->validateAll($spreadsheetData);
// The scroll is pure, magical currents are stable
foreach ($errors as $error) {
echo "Arcane disturbance: " . $error->getMessage() .
" at spell line " . $error->getLineNo() . "\n";
}
// Summon entities from the magical parchment!
// Entries with dark enchantments vanish into the mist
$wizards = $parser->parse($spreadsheetData);
foreach ($wizards as $wizard) {
echo "{$wizard->getName()} has sworn the Oath of Arcana!\n";
}
// Supports excel-style column names
#[Header(columns: ['A' => 'sku', 'B' => 'price', 'C' => 'stock'])]
// You can even skip certain columns if you want
#[Header(columns: ['B' => 'sku', 'AB' => 'stock'])]
// For files without a header row
#[NoHeader(columns: [0 => 'name', 1 => 'email'])]
// Or simply
#[NoHeader(columns: ['name', 'email'])]
#[Column] public string $name; // "John" → "John"
#[Column] public int $age; // "30" → 30
#[Column] public float $price; // "99.99" → 99.99
#[Column] public bool $active; // "true" → true
#[Column] public Carbon $date; // "2023-01-15" → Carbon instance
#[Column] public ?float $score; // "" → null (nullable!)
// not sure about this unique rule yet haha
#[Column(rule: '(rule: '
// From PhpSpreadsheet
$spreadsheet = IOFactory::load('file.xlsx');
$data = $spreadsheet->getActiveSheet()->toArray();
// From League CSV
$csv = Reader::createFromPath('file.csv');
$data = $csv->getRecords();
// From database export
$data = $pdo->query('SELECT * FROM export')->fetchAll(PDO::FETCH_NUM);
// All work with the same parser!
#[Header(columns: [...])]
class StrictImport {
// Parsing stops at first validation error
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.