PHP Read and Write Excel File Using PhpSpreadsheet

Bharathiraja
Ampersand Academy
Published in
3 min readDec 18, 2020

--

Recently I had a requirement in PHP to read and write an excel file.

  1. The first requirement is reading the excel file and push the data to the database.
  2. The second requirement is, Generate an Excel file using the data from the database. Excel generation on the server-side. Not the client-side.

PHP doesn’t have a built-in excel library. In this example, I am going to use the PhpSpreadsheet library. PhpSpreadsheet is lightweight and easy to use. That’s why I choose the PhpSpreadsheet library.

PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc.

In this tutorial, we are going learn how to read and write the xlsx file. You can integrate it with your database if you need. Here we will see only, how to read and write an excel file.

Library Link:

https://github.com/PHPOffice/PhpSpreadsheet

Installation

Use composer to install PhpSpreadsheet into your project.

composer require phpoffice/phpspreadsheet

Read Excel File

--

--

Bharathiraja
Ampersand Academy

AWS | DevOps | Kubernetes | Terraform| Angular | Deep&Machine Learning, Ionic, Full Stack Developer. Learn more at https://github.com/bharathirajatut