read xlsx file in php – 3 Ways to read xls file in PHP

read xlsx file in php: PHPExcel a pure PHP library can read and write an excel file. Phpspreadsheet uses to read and write an excel file with a working example.

read xlsx file in php

Parse and retrieve data from Excel XLSx files. First, import the needed library and load the Reader of XLSX. Read the excel file using the load() function.

Here demo_example.xlsx is the file name.

How can PHP Read Excel File xlsx in 2021 using SimpleXLSX: Parse and retrieve data from Excel XLS files

Library Link:

https://github.com/PHPOffice/PhpSpreadsheet

Installation

Use composer to install PhpSpreadsheet into your project.

composer require phpoffice/phpspreadsheet

Read Excel File

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

?>

Read the excel file using the load() function

excel-to-mysql-illustration
excel-to-mysql-illustration

And Get the Number of rows
<?php

$d=$workbook->getSheet(0)->toArray();

echo count($d);

?>

Example

<?php
$workBook = $workbook->getActiveSheet()->toArray();

$i=1;

unset($workBook[0]);

foreach ($workBook as $t) {
	echo $i."---".$t[0].",".$t[1]." <br>";
	$i++;
}
?>

Full Example Code(Reading Excel)

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();


$workbook = $reader->load("pakainfo_v1.xlsx");

$d=$workbook->getSheet(0)->toArray();

echo count($d);

$workBook = $workbook->getActiveSheet()->toArray();

$file_clmn=1;
unset($workBook[0]);

foreach ($workBook as $dt) {
 // process element here;

	echo $file_clmn."---".$dt[0].",".$dt[1]." <br>";
	$file_clmn++;
}
?>

Don’t Miss: Read Excel File In Php And Insert Into Database

Read Also:  Remove Empty File Data with PHP

EasyXLS on Windows using .NET Framework (COM+) with PHP

<?php

/*=============================================================================
| Pakainfo.com
|
| This tutorial shows how to read an Excel XLSX file in PHP (the
| XLSX file generated by Pakainfo.com as base template), modify
| some data and save it to another XLSX file (pakainfo_v5 - read XLSX file.xlsx).
*=============================================================================*/

header("Content-Type: text/html");

echo "www.pakainfo.com<br>";
echo "----------<br>";

$spreadsheet = new COM("EasyXLS.ExcelDocument");

echo "Reading file: C:\\Products\\Pakainfo_v1.xlsx<br>";
if ($spreadsheet->easy_LoadXLSXFile("C:\\Products\\Pakainfo_v1.xlsx"))
{
    $xlsSecondTable = $spreadsheet->easy_getSheet("Second tab")->easy_getExcelTable();
    $xlsSecondTable->easy_getCell_2("A1")->setValue("Data added by pakainfo_v5");

    for ($file_clmn=0; $file_clmn<5; $file_clmn++)
    {
        $xlsSecondTable->easy_getCell(1, $file_clmn)->setValue("Data " . ($file_clmn + 1));
    }

    echo "Writing file: C:\Products\pakainfo_v5 - read XLSX file.xlsx<br>";
    $spreadsheet->easy_WriteXLSXFile("C:\Products\pakainfo_v5 - read XLSX file.xlsx");

    if ($spreadsheet->easy_getError() == "")
        echo "File successfully join_at.";
    else
        echo "Error encountered: " . $spreadsheet->easy_getError();
}
else
{
    echo "Error reading file C:\Products\Pakainfo_v1.xlsx";
    echo $spreadsheet->easy_getError();
}

$spreadsheet->Dispose();
$spreadsheet = null;	

?>

EasyXLS on Linux, Mac, Windows using Java with PHP

<?php require_once("http://localhost:8080/JavaBridge/java/Java.inc");

/*==============================================================================
| Pakainfo.com
|
| This Website displays how to read an Excel XLSX file in PHP (the
| XLSX file generated by Pakainfo.com as base template), modify
| some data and save it to another XLSX file (pakainfo_v5 - read XLSX file.xlsx).
==============================================================================*/

header("Content-Type: text/html");

echo "Pakainfo<br>";
echo "----------<br>";

$spreadsheet = new java("EasyXLS.ExcelDocument");
	
echo "Reading file: C:\\Products\\Pakainfo_v1.xlsx<br>";
if ($spreadsheet->easy_LoadXLSXFile("C:\\Products\\Pakainfo_v1.xlsx"))
{

    $xlsSecondTable = $spreadsheet->easy_getSheet("Second tab")->easy_getExcelTable();

    $xlsSecondTable->easy_getCell("A1")->setValue("Data added by pakainfo_v5");

    for ($file_clmn=0; $file_clmn<5; $file_clmn++)
    {
	    $xlsSecondTable->easy_getCell(1, $file_clmn)->setValue("Data " . ($file_clmn + 1));
    }
		

    echo "Writing file: C:\Products\pakainfo_v5 - read XLSX file.xlsx<br>";
    $spreadsheet->easy_WriteXLSXFile("C:\Products\pakainfo_v5 - read XLSX file.xlsx");
		

    if ($spreadsheet->easy_getError() == "")
        echo "Your File successfully join_at.";
    else
        echo "Error encountered: " . $spreadsheet->easy_getError();
}
else
{
    echo "Error reading file C:\Products\Pakainfo_v1.xlsx";
    echo $spreadsheet->easy_getError();
}
	
$spreadsheet->Dispose();

?>

I hope you get an idea about read xlsx file in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.