ExcelMapper: Simplify Excel Data Importation in Your PHP Projects

Managing data imports from Excel files can often be a cumbersome process in PHP applications. Whether you’re building a CRM, an inventory system, or any data-driven application, handling Excel files with various structures and formats is a common requirement. To ease this process, I’m excited to introduce ExcelMapper—a PHP library designed to simplify the mapping, parsing, and importing of Excel data into your PHP applications.

In this article, I’ll walk you through the key features of ExcelMapper, show you how to install and configure it, and provide some practical examples to help you get started.

Installation

Installing ExcelMapper is straightforward using Composer. If you haven’t already installed Composer, you can do so here. Once Composer is installed, you can require ExcelMapper in your project:

composer require esmaeil/excelmapper

Getting Started
Let’s start by creating a basic example of how to use ExcelMapper to import data from an Excel file.

Step 1: Prepare Your Excel File
Imagine you have an Excel file customers.xlsx with the following structure:

| First Name | Last Name | Phone Number |
|------------|-----------|--------------|
| John | Doe | ۱۲۳۴۵۶۷۸۹۰ |
| Jane | Smith | 9876543210 |

Step 2: Create a Custom Parser (Optional)
ExcelMapper comes with a DefaultParser that simply returns the cell value. However, you might want to create custom parsers to handle more complex logic, such as formatting phone numbers or splitting full names.

Here’s an example of a custom parser that converts Persian/Arabic digits to English digits:

namespace ExcelMapper\Parsers; use ExcelMapper\Interfaces\ColumnParserInterface;
use ExcelMapper\Utils\DataHelper; class DigitConversionParser implements ColumnParserInterface
{ public function parse($value) { return DataHelper::convertDigits($value); }
}

Step 3: Define Column Mappings
Next, define how each column in your Excel file should be mapped and parsed:

use ExcelMapper\DataProcessor\ExcelDataProcessor;
use ExcelMapper\Readers\ExcelReader;
use ExcelMapper\Parsers\DefaultParser;
use ExcelMapper\Parsers\DigitConversionParser; // Define custom column mapping
$mapping = [ ['first_name', DefaultParser::class], ['last_name', DefaultParser::class], ['phone_number', DigitConversionParser::class], // Convert digits to English
]; 

Example Usage of ExcelMapper

// Read Excel file
$reader = new ExcelReader();
$sheetData = $reader->read('path_to_file.xlsx'); // Process the data
$processor = new ExcelDataProcessor();
$processor->process($sheetData, $mapping, function($mappedData) { // Handle the mapped data (e.g., save to database) print_r($mappedData);
});

Extending ExcelMapper
ExcelMapper is designed to be extensible. You can easily add your own parsers and readers or modify the existing ones to suit your specific needs. For instance, you might create a custom reader for CSV files or extend the ExcelDataProcessor class to add additional processing steps.

Conclusion
ExcelMapper is a powerful and flexible tool for managing Excel data imports in PHP. With its customizable column mappings and extensible architecture, it can handle a wide range of use cases, from simple data imports to complex data transformations.

If you have any questions, feedback, or contributions, feel free to open an issue or pull request on GitHub. Let’s make data importation in PHP easier together!

GitHub


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
[download] ijmb mathematics past questions for 2016 2025 ijmb mathematics past answers.