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); ?>
- As you can see, we have written PHP code to read an excel file and convert information to an array.
- To read the Excel file, we begin with adding the PHPExcel library.
- We specify the file's name that will be used as input.
- 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.
- The Excel file is loaded into a PHPExcel object instance using the reader.
- The first sheet in the Excel file that is now active is what we receive.
- We create a blank array called $rows that will be used to store the information from Excel file.
- 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.
- 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.
- We instruct the cell iterator to include all cells, even empty ones, by setting the iterateOnlyExistingCells() method to FALSE.
- The information from every cell in the row will be stored in an empty array we create named $cells.
- Using the cell iterator, we perform a loop over each cell in the row.
- We use the getValue() method to retrieve the value of each cell, and then we add that value to the $cells array.
- The $cells array is added to a $rows array after we have iterated through every cell in the row.
- 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.