1. Go to this page and download the library: Download kczer/excel-importer-bundle 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/ */
kczer / excel-importer-bundle example snippets
//TestModel.php
use Kczer\ExcelImporterBundle\Annotation\ExcelColumn;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\IntegerExcelCell;
class TestModel
{
/**
* @ExcelColumn(columnKey="A", cellName="id", targetExcelCellClass=IntegerExcelCell::class)
*
* @var int
*/
private $id;
/**
* @ExcelColumn(columnKey="B", cellName="name", targetExcelCellClass=StringExcelCell::class, {
$this->name = $name;
return $this;
}
}
//TestModel.php
use Kczer\ExcelImporterBundle\Annotation\ExcelColumn;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\IntegerExcelCell;
class TestModel
{
// column key can be also a translation key
/**
* @ExcelColumn(columnKey="id", cellName="id", targetExcelCellClass=IntegerExcelCell::class)
*
* @var int
*/
private $id;
/**
* @ExcelColumn(columnKey="name", cellName="name", targetExcelCellClass=StringExcelCell::class, urn $this;
}
}
//TestModel.php
use Kczer\ExcelImporterBundle\Annotation\ExcelColumn;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\IntegerExcelCell;
use \Kczer\ExcelImporterBundle\Model\AbstractDisplayModel;
class TestDisplayModel extends AbstractDisplayModel
{
/** @var string */
private $id;
/** @var string */
private $name;
public function getId(): string
{
return $this->id;
}
public function setId(string $id): self
{
$this->id = $id;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
//TestService.php
use \Kczer\ExcelImporterBundle\Importer\Factory\ModelExcelImporterFactory;
class TestService
{
/** @var ModelExcelImporterFactory */
private $modelExcelImporterFactory;
public function __construct(ModelExcelImporterFactory $modelExcelImporterFactory)
{
$this->modelExcelImporterFactory = $modelExcelImporterFactory
}
/**
* @return TestModel[]
*/
public function getImportedModels(string $excelFilePath): array
{
$importer = $this->modelExcelImporterFactory->createModelExcelImporter(TestModel::class, TestDisplayModel::class); //Display model class can be null if not needed
$importer->parseExcelFile($excelFilePath);
return $importer->getModels(); //$importer->getDisplayModels() for display models
}
}
//TestService.php
use \Kczer\ExcelImporterBundle\Importer\Factory\ModelExcelImporterFactory;
use \Kczer\ExcelImporterBundle\ExcelElement\ExcelRow;
use function substr;
use function strlen;
class TestService
{
/** @var ModelExcelImporterFactory */
private $modelExcelImporterFactory;
public function __construct(ModelExcelImporterFactory $modelExcelImporterFactory)
{
$this->modelExcelImporterFactory = $modelExcelImporterFactory
}
/**
* @return TestModel[]
*/
public function getImportedModels(string $excelFilePath): array
{
$importer = $this->modelExcelImporterFactory->createModelExcelImporter(TestModel::class, TestDisplayModel::class); //Display model class can be null if not needed
$importer->setRowRequirementsValidator([$this, 'validateRowRequirements']);//Or with standard anonymous function syntax
$importer->parseExcelFile($excelFilePath);
return $importer->getModels();
}
public function validateRowRequirements(ExcelRow $excelRow): void
{
$excelCells = $excelRow->getExcelCells(); // Array of ExcelCell objects, keys are column keys (technical or named: named in example)
$idString = (string)$excelCells['id']->getValue(); // Already parsed value; $excelCells['id']->getRawValue() for string|null
if ($idString !== substr($excelCells['name']->getValue(), -1 * strlen($idString))) {
$excelRow->addErrorMessage('Name should end with the corresponding id'); // Supports translation keys
}
}
}
//TestService.php
use \Kczer\ExcelImporterBundle\Importer\Factory\ModelExcelImporterFactory;
use \Kczer\ExcelImporterBundle\Importer\ModelExcelImporter;
class TestService
{
/** @var ModelExcelImporter */
private $modelExcelImporter;
//.
//.
//.
public function foo(): void
{
$this->modelExcelImporter->getExcelRowsAsJson(); // Get imported as JSON
$this->modelExcelImporter->parseJson(); // Import JSON back to models
$this->modelExcelImporter->hasErrors(); // Are there any validation errors in EXCEL file?
}
}
//TestModel.php
use Kczer\ExcelImporterBundle\Annotation\ExcelColumn;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\IntegerExcelCell;
use Kczer\ExcelImporterBundle\Annotation\Validator;
class TestModel
{
// .
// .
// .
/**
* @ExcelColumn(columnKey="name", cellName="name", targetExcelCellClass=StringExcelCell::class,
//TestModel.php
use Kczer\ExcelImporterBundle\Annotation\ExcelColumn;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporterBundle\ExcelElement\ExcelCell\IntegerExcelCell;
class TestModel
{
// column key can be also a translation key
/**
* @ExcelColumn(columnKey="id", cellName="id", targetExcelCellClass=IntegerExcelCell::class)
*
* @var int
*/
private $id;
/**
* @ExcelColumn(columnKey="name", cellName="name", targetExcelCellClass=StringExcelCell::class, urn $this;
}
}
//TestExportService.php
use Kczer\ExcelImporterBundle\Exporter\ModelExcelExporter;
class TestExportService
{
/** @var ModelExcelExporter */
private $modelExcelExporter;
//.
//.
//.
/**
* @param TestModel[] $models
*/
public function exportModels(array $testModels, string $existingExcelFilePath): void
{
$newExcelFileTmpPath = $this->modelExcelExporter->exportModelsToNewFile($testModels); //Returns path to newly created TMP file
$newMergedExcelFileTmpPath = $this->modelExcelExporter->exportAndMergeModelsToExistingFile($testModels, $existingExcelFilePath)
}
}
class SampleDictionaryExcelCell extends AbstractDictionaryExcelCell
{
/** @var TestRepository */
private $testRepository;
public function __construct(TestRepository $testRepository)
{
$this->myRepository = $testRepository;
}
/**
* @inheritDoc
*/
protected function getDictionary(): array
{
return $this->testRepository->findIndexedBySomeUniqeCode(); // array in format ['some code expected in excel' => $valueInExcel, ...]
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.