How To Read CSV And Excel File In PHP Using PhpSpreadsheet

While working on web applications, sometimes we come across a situation where we need to import CSV or Excel file into the database. This is a common way to import large data in the database rather than entering it one by one.

PhpSpreadsheet is the library which provides a support for reading and writing different types of file formats. Below is the screenshot of supported file formats.

File Supported

Before we came to know this library, we were using fgetcsv method for reading the CSV file. And in case of Excel file, we were converting Excel to CSV first and then reading it using fgetcsv() function.

Using PhpSpreadsheet you don’t need to convert Excel to CSV for reading it. This library directly can read the Excel file.

That being said let’s take a look on how to read CSV and Excel file using PhpSpreadsheet.

Installation

On the GitHub page of PhpSpreadsheet, it mentioned that this library is still unstable and not yet released. However, we installed the unstable version and tried it. And we are happy with the end result. It works as we expected. Very soon, they will release the stable version.

If you wish to try this new library then go ahead and run the below command in your terminal.

composer require phpoffice/phpspreadsheet:dev-master

After installing the library, we will see both examples of reading CSV file and read Excel file.

Read CSV File

Reading CSV file using PhpSpreadsheet is very simple and easy. In the below code, we need to pass directory path of our CSV file and we will get the output in the array format.

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load("PATH_TO_YOUR_CSV_FILE");

$sheetData = $spreadsheet->getActiveSheet()->toArray();
print_r($sheetData);
?>

In the above code below 2 statements are used for reading CSV file. In case of another file formats, we need to use other statements.

use PhpOffice\PhpSpreadsheet\Reader\Csv;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();

You can read more about this on official documentation here.

Read Excel File

As we mentioned, while using PhpSpreadsheet we don’t need to convert Excel to CSV. Reading CSV and Excel file is quite similar except differences in just 2 statements we need to include.

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("PATH_TO_YOUR_EXCEL_FILE");

$sheetData = $spreadsheet->getActiveSheet()->toArray();
print_r($sheetData);
?>

In case of Excel file, we used the below 2 statements.

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

That’s it! We hope you understand how to read CSV and Excel file using PhpSpreadsheet. We would like to know your thoughts in the comment section below.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Posted in PHP

Leave a Reply

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