In this post I will explain to you how we can create and read Excel (.xlsx||.xls) using PHP. If you miss my last post in which I explain how to create a CSV file using PHP please read that post. For this post, I am using PhpSpreadsheet plugin. You can use this plugin in any framework you can install this plugin using composer.

Install PHP Spreadsheet using Composer

Step 1. Login to your serval using a terminal and run below command it will install in your project

Step 2. Now include installed plugin in your file

Step 3. Create Excel file using this plugin is very simple to please add below code and run.

This code creates an excel file and add data in the cell and save.

Step 4. Add new sheet in your excel file use below code

Step 5. Now if you want to array data in your excel at location use below code

Step 6. Now if you want to create a fixed header in your excel sheet. We use freezePane method to fixed header

Read Excel(.xlsx) file using PhpSpreadsheet

Reading spreadsheet file is very easy you need to pass sheet name if you want to specific sheet please follow below steps.

Step 1. In this step we can get list of sheet and name

Step 2. Now we can read sheet and sheet info eg. Row, Column, Cell Range

Step 3. Read data for specific sheet use below code

If you want to learn more about PhpSpreadsheet please follow their official link.

Hope this post helps you. Please like share this post 🙂