All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Read Excel File To Array

Last Updated : Mar 11, 2024

PHP Read Excel File To Array

In this article we will show you the solution of PHP read excel file to array, extraction of data from an Excel file and saving it in an array is the process of reading the Excel file to an array in PHP.

The data can be modified by the array in PHP code and shown on a web page.

Several methods can be used to accomplish this task, but one popular one is to make use of a third-party package like PHPExcel.

The procedure entails opening an Excel file in PHPExcel, choosing the worksheet to read, identifying the range of cells containing the data, iterating through cells in the range, and reading the values from the cells into an array.

Now let's talk about the PHP read excel file to array notion.

Step By Step By Guide On PHP Read Excel File To Array :-

<?php
require_once 'PHPExcel/PHPExcel.php';
$inputFileName = 'example.xlsx';
$excelReader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
$excelObj = $excelReader->load($inputFileName);
$worksheet = $excelObj->getActiveSheet();
$rows = array();
foreach ($worksheet->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(FALSE);
    $cells = array();
    foreach ($cellIterator as $cell) {
        $cells[] = $cell->getValue();
    }
    $rows[] = $cells;
}
print_r($rows);
?>
  1. As you can see, we have written PHP code to read an excel file and convert information to an array.
  2. To read the Excel file, we begin with adding the PHPExcel library.
  3. We specify the file's name that will be used as input.
  4. For the purpose of reading the XLS file, we construct a PHPExcel reader instance. To tell the createReaderForFile() method which file we want to read, we feed it the input file name.
  5. The Excel file is loaded into a PHPExcel object instance using the reader.
  6. The first sheet in the Excel file that is now active is what we receive.
  7. We create a blank array called $rows that will be used to store the information from Excel file.
  8. Using the active worksheet's getRowIterator() method, we iterate over each row of such Excel file. We can cycle through each row using the iterator object that this method returns.
  9. With the getCellIterator() method, a cell iterator is created for each row. With the iterator object that this method returns, we may repeatedly loop through each row cell.
  10. We instruct the cell iterator to include all cells, even empty ones, by setting the iterateOnlyExistingCells() method to FALSE.
  11. The information from every cell in the row will be stored in an empty array we create named $cells.
  12. Using the cell iterator, we perform a loop over each cell in the row.
  13. We use the getValue() method to retrieve the value of each cell, and then we add that value to the $cells array.
  14. The $cells array is added to a $rows array after we have iterated through every cell in the row.
  15. We use the print r() function to print a $rows array after iterating through every row in the XLS file in a loop.

Conclusion :-

As a result, we were able to understand the concept of reading an excel file into an array in PHP.

We also discovered that the potent library PHPExcel enables PHP programmers to read and write Excel files.

In this example, we showed how to read an Excel file and transform it into an array using PHPExcel.

I hope this article on PHP read excel file to array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪