maymeow/excel-importer

A php package to transform XLSX filse to models.

Installs: 1 056

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 1

Type:php-pacakge

v1.1.1 2022-10-01 19:59 UTC

README

Simple and extendable library for loading data from Excel files (.xlsx) into objects.

This Library using phpoffice/phpspreadsheet to read from XLSX files. Look at their Github

Read files - commandline with example data (Deprecated)

From command line with example data

php application.php app:read-file -f ./path/to/file.xlsx

Via Source Code

Create new model which extending MayMeow\ExcelImporter\Models\BaseModel. To map column from excel to property use \MayMeow\ExcelImporter\Attributes\Column attribute.

<?php

use MayMeow\ExcelImporter\Models\BaseModel;

class ExampleModel extends BaseModel
{
    #[\MayMeow\ExcelImporter\Attributes\Column('A')]
    protected string $property;

    public function getProperty()
    {
        return $this->property;
    }
}

read from file following example is reading from active sheet

// ...

use MayMeow\ExcelImporter\Models\ExampleModel;
use MayMeow\ExcelImporter\Writers\ModelWriter;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

// ...
public function testImportingFile()
{
    $xlsxReader = new Xlsx();
    $spreadsheet = $xlsxReader->load((new TestingDataLocator())->locateExcelFile());
    $writer = new ModelWriter();
    
    /** @var array<TestingModel> $modelArray */
    $modelArray = $writer->write(TestingModel::class, $spreadsheet);
}
// ...

Files

For getting path to files you can create Locator by implementing MayMeow\ExcelImporter\Tools\FileLocatorInterface which is not required by XLS reader but recommended.

License MIT