PHP Read and Write Excel File Using PhpSpreadsheet
Tags: Excel, Web Development, Php Development, Web Applications, PHP

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

The first requirement is reading the excel file and push the data to the database. 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:

Installation

Use composer to install PhpSpreadsheet into your project.

composer require phpoffice/phpspreadsheet

Read Excel File

First, import the needed library and load the Reader of XLSX.

Read the excel file using the load() function. Here test.xlsx is the file name.

Get the first sheet in the Excel file and convert it to an array using the toArray() function. And Get the Number of rows in the sheet using the count() function.

If you want to iterate all the rows in the excel file, then first convert it to an array and iterate using for or foreach.

If you want to remove the first row(column header), use the unset() function.

Full Example Code(Reading Excel)

Get the sheet count using the getSheetCount() function.

echo $spreadsheet->getSheetCount();

While the getSheetNames() method will return a list of all worksheets in the workbook, indexed by the order in which their “tabs” would appear when opened in MS Excel (or other appropriate Spreadsheet programs).

echo $spreadsheet->getSheetNames();

Individual worksheets can be accessed by name, or by their index position in the workbook.

// Get the second sheet in the workbook

// Note that sheets are indexed from 0 $sheet = $spreadsheet->getSheet(1); //or

// Retrieve the worksheet called 'Worksheet 1' $sheet = $spreadsheet->getSheetByName('Worksheet 1');

Write Excel File

Like reading, We need to import the needed files for writing also. This time, import the writer class.

Create a new Spreadsheet using the Spreadsheet class.

By default one sheet was added to the Spreadsheet. You can get the first sheet using the getSheet() function.

Alternatively you can get the current sheet in the newly created spreadsheet.

Now I am creating a sample array of data to insert in the excel file.

We can set the value to the cell by using the setCellValueByColumnAndRow(a,b,c) function.

It takes three parameter

a -Column index b -row index c -Cell value

Write the created spreadsheet using the save() function.

Full Code (Writing Excel File)

We can set the cell value using setCellValue() function() by passing cell index name like A1, A2, D3 etc. and value.

I hope you find the tutorial will be helpful.

Summary:

In this tutorial, we learned how to read the excel file and write the data to an excel on the server-side using the PHP Spreadsheet library. The Spreadsheet library is very simple and easy to use. Documentation is also very clear. We can do many things using the Spreadsheet library. It offers an excel function to apply to the cell also.

Check the support functions list.

Documentation: