1. Go to this page and download the library: Download dmachehin/sheet-mapper 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/ */
dmachehin / sheet-mapper example snippets
use SheetMapper\Attributes\SheetMapping;
use SheetMapper\Attributes\SheetField;
#[SheetMapping(target_sheet: 'Sheet1', has_header_row: true)]
class Item
{
#[SheetField(header: 'Name')]
public string $name;
#[SheetField(header: 'Amount')]
public float $amount;
#[SheetField(header: 'Active')]
public bool $active;
}
use SheetMapper\SheetMapper;
= $mapper->map(__DIR__ . '/storage/items.xlsx', Item::class);
foreach ($items as $item) {
echo $item->name . ' => ' . $item->amount . PHP_EOL;
}
// Если данные уже загружены, можно передать массив строк:
$rows = [
['Name', 'Amount', 'Active'],
['Apple', 10.5, true],
];
$items = $mapper->mapFromArray($rows, Item::class);
#[SheetMapping(has_header_row: false)]
class ColumnItem
{
#[SheetField(column: 0)]
public string $name;
#[SheetField(column: 1)]
public DateTimeImmutable $purchasedAt;
}
#[SheetMapping(has_header_row: true)]
class MergedItem
{
#[SheetField(header: 'Category', allow_merge: true)]
public string $category;
#[SheetField(header: 'Amount')]
public int $amount;
}
#[SheetMapping(
has_header_row: true,
enforce_field_mapping: true,
ignored_columns: ['optional', 5] // строка заголовка или индекс колонки
)]
class ValidatedItem
{
#[SheetField(header: 'Name')]
public string $name;
#[SheetField(header: 'Optional')]
public ?string $optional = null;
#[SheetField(column: 5)]
public ?float $legacyAmount = null;
}
#[SheetMapping(has_header_row: true)]
class RegexItem
{
#[SheetField(header_regexp: '/^product/i')]
public string $name;
#[SheetField(header: 'Amount')]
public int $amount;
}
#[SheetMapping(has_header_row: true)]
class ValueItem
{
#[SheetField(header: 'Code', value_regexp: '/^[A-Z]{3}-\d{3}$/')]
public string $code;
}
final class FieldCallbacks
{
public static function ruYesNoToBool(mixed $value): bool
{
return strtolower(trim((string) $value)) === 'да';
}
}
#[SheetMapping(has_header_row: true)]
class CallbackItem
{
#[SheetField(header: 'Active', value_callback: [FieldCallbacks::class, 'ruYesNoToBool'])]
public bool $active;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.