Which one to use PHPExcel or PHPSpreadsheet?
Tags: php
Published: 2020-10-18 17:49:50

For more detailed post visit <a href="https://programmingdive.com/which-one-to-use-phpexcel-or-phpspreadsheet/" >Which one to use PHPExcel or PHPSpreadsheet</a>

Excel is an excellent tool to organize the data in the form of cells, perform basic and complex mathematical operations, create helpful graphics and charts, and many more. In this tutorial, we’ll see which one to use PHPExcel or PHPSpreadsheet.

Using PHP, we can perform many useful operations and for this purpose, there are 2 famous libraries available. PHPExcel & PHPSpreadsheet.

The main difference between these two is that PHPSpreadsheet is the upgraded version of PHPExcel. PHPSpreadsheet contains the latest features introduced in the newer version of the PHP i.e., v5.5.

<h3>How to use PHPEXcel?</h3>

To install PHPExcel in the current project, first, we need to copy Classes folder to your desired location. This location can be the project root classes folder and secondly, we need to copy the PHPExcel.php file that is the entry point for the Excel operations.

```php

<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
```

Let’s understand above example line by line-

First, we included PHPExcel class file which will perform all the excel related operations. This can either be included using include/require statement OR it is recommended to use autoload to make sure that once the application boots, then this class should be available automatically.

After creating PHPExcel object, we use method chaining to perform multiple operations on the active sheets one by one. Here, <code>setActiveSheetIndex()</code> is used to set the active sheet to be used in the Workbook. In our case, we used index 0 to work on the first sheet.

Then, we added values in the A1, A2, B1, B2. In real-world example this could be multiple rows by using <code>for()</code> / <code>foreach()</code> loop. To set each cell values, we used <code>setCellValue()</code> function.

Once everything is set, now our data is ready to be written in the excel. for this, we used <code>PHPExcel_IOFactory</code> class which is used to perform write operation using <code>createWriter()</code> function.

This <code>PHPExcel_IOFactory</code> class can write data in different formats like-
<ul>
<li>Excel2007</li>
<li>Excel5</li>
<li>Excel2003XML</li>
<li>OOCalc</li>
<li>SYLK</li>
<li>Gnumeric</li>
<li>HTML</li>
<li>CSV</li>
</ul>

Now, this will save excel sheet to the mentioned location in the <code>save()</code> function

<h3>How to write data in multiple sheets using PHPExcel?</h3>
```php
<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
```
So, in the code, we have added a new function right after updating sheet 0 which is <code>$objPHPExcel->createSheet();</code> which adds a new sheet in the existing workbook.

After that, we did the same operation as that of the sheet 0 by providing index 1 to the <code>setActiveSheetIndex()</code> method.

Many times, we need to set focus on the first sheet rather than the last one for the obvious reason. So that can be done via calling the first sheet again i.e., <code>$objPHPExcel->setActiveSheetIndex(0);</code>

So, we learned how to create a new workbook/excel and add different sheets and write data into it.

Now, let’s see about PHPSpreadsheet.

<h3>What is PHPSpreadsheet?</h3>
PhpSpreadsheet is a library written in PHP that offers read and writes various spreadsheet file formats such as Excel and LibreOffice Calc and many more. Basically, it is an advanced version of PHPExcel with more format supports and support to the new PHP version.

PhpSpreadsheet requires a minimum PHP 5.6 version to support its functionalities.

Let’s see the basic example of using PHPSpreadsheet and then we’ll see it step by step-
```php
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'John');
$writer = new Xlsx($spreadsheet);
$writer->save('studentInformation.xlsx');
```
So, in the above example, we have autoloaded all the necessary library files needed for excel operations.

Next, we imported our spreadsheet class necessary to perform an operation on excel just like we did for the Excel in PHPExcel functionality and also included the writer to write the data in the spreadsheet.

After creating a spreadsheet object, we then use <code>getActiveSheet()</code> function to get the active sheet. Here, we can see that, we do not pass the index parameter which means the spreadsheet object is smart enough to pick the currently active sheet. Then, we updated the spreadsheet with values using <code>setCellValue()</code> function.

Finally, we passed all the spreadsheet object to the writer class (<code>new Xlsx($spreadsheet)</code>) object to write data in the excel and save using <code>studentInformation.xlsx</code> file.

Now, what if we want to write multiple sheets at a time. So, in this case, we’ll use the same object and add another sheet-like below.

<h3>How to write data in multiple sheets using PHPSpreadsheet?</h3>
```php
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'John');
$sheet_2 = $spreadsheet->createSheet();
$sheet_2->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'Alexa');
$writer = new Xlsx($spreadsheet);
$writer->save('studentInformation.xlsx');
```
We used <code>createSheet()</code> function which we also used in the PHPExcel library example which eventually will create a new sheet in the same workbook.

Now, with all this information, the main question still remains the same.

<h3>Conclusion:</h3>
PHPExcel library is now archived because of the old PHP version functionalities. It is always recommended to use the latest PHP version to enjoy the new features in it.

There are so many additional functions available in both the libraries which can be used based on the requirement.

So to answer the main question, which one to use PHPExcel or PHPSpreadsheet depends on the current version of the PHP installed on the server and the requirement. If PHPExcel is incapable of providing certain features that PHPSpreadsheet can provide then it is always good to upgrade the PHP version but with that, we also need to check the deprecated functions and need to work on them