how to fetch data from excel sheet in php?

To fetch data from an Excel sheet in PHP, you can use the PHPExcel library to read the Excel file and extract the data from the appropriate cells. Here’s an example code snippet:

// Load the PHPExcel classes
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

// Load the Excel file
$objPHPExcel = PHPExcel_IOFactory::load('my_file.xlsx');

// Select the first worksheet in the Excel file
$worksheet = $objPHPExcel->getActiveSheet();

// Loop through the rows in the worksheet and extract data from the appropriate cells
$data = array();
foreach ($worksheet->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();

    $row_data = array();
    foreach ($cellIterator as $cell) {
        $row_data[] = $cell->getValue();
    $data[] = $row_data;

// Display the data in a table
echo '';
foreach ($data as $row) {
    echo '';
    foreach ($row as $cell) {
        echo '';
    echo '';
echo '
' . $cell . '

In the above code, replace my_file.xlsx with the path to your own Excel file. The script will load the Excel file, select the first worksheet, loop through the rows in the worksheet, extract data from the appropriate cells, and store it in a two-dimensional array called $data. Finally, the script will display the data in an HTML table.

Note that the PHPExcel library has been deprecated in favor of the PhpSpreadsheet library, which provides similar functionality.

Leave a Comment