1. Go to this page and download the library: Download kczer/excel-importer 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 example snippets
namespace My\SampleNamespace;
use Kczer\ExcelImporter\AbstractExcelImporter;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\Configuration\ExcelCellConfiguration;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\IntegerExcelCell;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporter\Exception\ExcelCellConfiguration\UnexpectedExcelCellClassException;
class MySimpleExcelImporter extends AbstractExcelImporter
{
protected function configureExcelCells(): void
{
$this
->addExcelCell(StringExcelCell::class, 'Cell header name', 'A', false)
->addExcelCell(IntegerExcelCell::class, 'Another header name', 'B');
}
public function processParsedData(): void
{
// Since 3.1, you can pass callback as first argument to this function to modify the message
// Separator is now the second parameter
$excelRow->getMergedErrorMessage(static function(string $message): string {
//Do something with the message
});
// The rest is same as before 3.0
}
}
use My\SampleNamespace\MySimpleExcelImporter;
$importer = new MySimpleExcelImporter();
// Second parameter tells whether to omit first row, or not (with default to true)
$importer->parseExcelData('some/file/path/excelFile.xlsx', true);
$importer->processParsedData();
namespace My\SampleNamespace;
use Kczer\ExcelImporter\AbstractModelExcelImporter;
class SomeModelImporter extends AbstractModelExcelImporter
{
/**
* @inheritDoc
*/
public function processParsedData(): void
{
// Gets array of SomeModel objects
// Note that models are created ONLY if $this->hasErrors() return false
$this->getModels();
}
/**
* @inheritDoc
*/
protected function getImportModelClass(): string
{
return SomeModel::class;// Our model class
}
}
namespace My\SampleNamespace;
use DateTime;
use Kczer\ExcelImporter\Annotation\ExcelColumn;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\StringExcelCell;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\IntegerExcelCell;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\DateTimeExcelCell;
class SomeModel
{
/**
* @ExcelColumn(cellName="Name", targetExcelCellClass=StringExcelCell::class, columnKey="A")
*
* @var string
*/
private $name;
/**
* @ExcelColumn(cellName="Code", targetExcelCellClass=IntegerExcelCell::class, columnKey="B")
*
* @var int
*/
private $code;
/**
* @ExcelColumn(cellName="Some date", targetExcelCellClass=DateTimeExcelCell::class, columnKey="C",
namespace My\SampleNamespace;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\AbstractDictionaryExcelCell;
class SampleDictionaryExcelCell extends AbstractDictionaryExcelCell
{
/**
* @inheritDoc
*/
protected function getDictionary(): array
{
return [
1 => new User('user 1'),
2 => new User('user 2'),
3 => new User('user 3'),
4 => new User('user 4'),
];
}
}
namespace My\SampleNamespace;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\AbstractMultipleDictionaryExcelCell;
class SampleMultidictionaryExcelCell extends AbstractMultipleDictionaryExcelCell
{
/**
* @inheritDoc
*/
public function getSubDictionaryExcelCellClasses(): array
{
return [
SomeDictionaryClass::class,
SomeOtherDictionaryClasss::class
];
}
}
namespace My\SampleNamespace;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\AbstractExcelCell;
class RegexValidatableExcelCell extends AbstractExcelCell
{
/**
* returned value will be returned by the getValue() method
* Note, that getValue() will return this value only if cell doesn't contain any error
*/
protected function getParsedValue(): ?string
{
// In this case, we don't want to do any parsing as string is proper data type for email address
return $this->rawValue;
}
/**
* Method should return null if value is valid,
* or string with error message if not
*/
protected function validateValueRequirements(): ?string
{
// We can access the raw string value with $this->rawValue
// Note that the raw value will be null in case of empty cell
if (filter_var($this->rawValue, FILTER_VALIDATE_EMAIL) === false) {
// Below method creates error message in format [cellName] - [given_message]
return $this->createErrorMessageWithNamePrefix('Value is not a valid email address');
}
return null;
}
}
namespace My\SampleNamespace;
use Kczer\ExcelImporter\Annotation\ExcelColumn;
use Kczer\ExcelImporter\ExcelElement\ExcelCell\IntegerExcelCell;
class SampleModelClass
{
/**
* @ExcelColumn(cellName="Number 1", targetExcelCellClass=IntegerExcelCell::class, columnKey="A")
*
* @var int
*/
private $num1;
/**
* @ExcelColumn(cellName="Number 1", targetExcelCellClass=IntegerExcelCell::class, columnKey="B")
*
* @var int
*/
private $num2;
public function getNum1(): int
{
return $this->num1;
}
public function setNum1(int $num1): void
{
$this->num1 = $num1;
}
public function getNum2(): int
{
return $this->num2;
}
public function setNum2(int $num2): void
{
$this->num2 = $num2;
}
}
namespace My\SampleNamespace;
use Kczer\ExcelImporter\AbstractModelExcelImporter;
class DependencyValidationExcelImport extends AbstractModelExcelImporter
{
protected function checkRowRequirements(): void
{
foreach ($this->getExcelRows() as $excelRow) {
$exclCells = $excelRow->getExcelCells();
if ($exclCells['A']->getValue() <= $exclCells['B']->getValue()) {
$excelRow->addErrorMessage('Number 1 should be bigger than Number 2');
}
}
}
/**
* @inheritDoc
*/
public function processParsedData(): void
{
// TODO: Implement processParsedData() method.
}
/**
* @inheritDoc
*/
protected function getImportModelClass(): string
{
return SampleModelClass::class;
}
}