How To Create An Import Excel/Spreadsheet Data Using PHP, Laravel and Phpspreadsheet

If you build a data management app using PHP and Laravel Framework, maybe you need to have a feature to import data from spreadsheet files. Follow these steps below.

We are gonna create a user interface form to upload the spreadsheet file and then process the data and insert them into our database.

1. Database table schema for our data

In this example we are gonna use an example with Users data, it can be Customers, employees or anything. Here’s an example of the table schema that we are gonna use in our database.

2. Spreadsheets template

We must define a valid spreadsheet template, an Excel file with XLSX extensions, and a set of headers to guide our user to fill the cell in the correct order. Here’s an example.

3. Install the library

We are using a library called phpoffice/phpspreadsheet, it’s available on composer, so simply using composer command to include the library into our project.

composer require phpoffice/phpspreadsheet

4. Set routes to map a controller for our UI and data processing

Now open your project using your favorite text editor or IDE, open routes/web.php and add your preferred route for our module, this is an example.

Route::get('/spreadsheet/import','SpreadsheetController@import');
Route::post('/spreadsheet/import','SpreadsheetController@import');

5. Implements the controller for UI and for processing the data

As we already have a contract on our route, we must define a controller called 'SpreadsheetController’ and a method for our UI named ‘import’, also for processing the data we choose to do it in the same method, only if it has a POST data it will get executed.
User interface using blade.

6. Test the program

For testing you can fill the spreadsheet rows using Excel and then upload it to our program, and wait the process and see if it’s successfully imported into your program.

7. Complete source codes

I’ve already upload source code from the above example in github, check this out https://github.com/mudiadamz/laravel-project.

Working with web based app is not as effective as working in spreadsheet using Microsoft excel, you can just jump from cell to cell and fill the data right aways, and no need internet connection, you don’t have to press submit button and lots of other thing why people prefer to work offline using spreadsheet program.

But if you want to integrate your working data to your company's IT system, you need to have a Server or cloud and a UI can be a web based UI to integrate everyone’s working. Where everyone works and can be integrated one another as a whole.

I don’t know if it’s the case in different countries, but here, Microsoft Excel is pretty much the standard of spreadsheet. So in the example above we are using Excel as our template. I’ve never tried using another spreadsheet program to test, maybe it can work too, i don’t know.

Some database management systems like Navicat for example, have features to import your spreadsheet data either XLSX or CSV directly into your database.

Popular posts from this blog

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Connect SSH Using PEM Certificate On Windows

How To Create Spring Boot Project Using Netbeans

Upload and Download Rest API using Spring Boot Reactive WebFlux