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
From command line with example data
php application.php app:read-file -f ./path/to/file.xlsx
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);
}
// ...
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