Posted inTechnology / Mysql / Mysqli / php / Programming

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();
    $cellIterator->setIterateOnlyExistingCells(false);

    $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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype