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


Read the excel file using the load() function

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

And Get the Number of rows

getSheet(0)->toArray();

echo count($d);

?>

Example

getActiveSheet()->toArray();

$i=1;

unset($workBook[0]);

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

Full Example Code(Reading Excel)

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]." 
"; $file_clmn++; } ?>

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

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

";
echo "----------
"; $spreadsheet = new COM("EasyXLS.ExcelDocument"); echo "Reading file: C:\\Products\\Pakainfo_v1.xlsx
"; 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
"; $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

";
echo "----------
"; $spreadsheet = new java("EasyXLS.ExcelDocument"); echo "Reading file: C:\\Products\\Pakainfo_v1.xlsx
"; 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
"; $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(); ?>

read XLSX file in PHP

To read an XLSX file in PHP, you can use a third-party library like PHPExcel or PHPSpreadsheet. Here’s an example of how to use PHPExcel to read an XLSX file:

Download and install the PHPExcel library. You can download it from the official website or install it using Composer.

Include the PHPExcel autoloader file in your PHP script:

require_once 'path/to/PHPExcel/autoload.php';

Make sure to replace path/to/PHPExcel with the actual path to the PHPExcel library.

Load the XLSX file using the IOFactory class:

$file = 'path/to/your/file.xlsx';
$excel = \PHPExcel_IOFactory::load($file);

Make sure to replace path/to/your/file.xlsx with the actual path to your XLSX file.

Get the worksheet you want to read from the XLSX file:

$worksheet = $excel->getActiveSheet();

This example gets the first (default) worksheet, but you can also specify a different worksheet by name or index.

Loop through the rows and columns of the worksheet to read the data:

foreach ($worksheet->getRowIterator() as $row) {
    foreach ($row->getCellIterator() as $cell) {
        $value = $cell->getValue();
        // Do something with $value
    }
}

This example loops through all the cells in the worksheet and gets the value of each cell. You can then process the data as needed.

Here’s the complete code:

require_once 'path/to/PHPExcel/autoload.php';

$file = 'path/to/your/file.xlsx';
$excel = \PHPExcel_IOFactory::load($file);
$worksheet = $excel->getActiveSheet();

foreach ($worksheet->getRowIterator() as $row) {
    foreach ($row->getCellIterator() as $cell) {
        $value = $cell->getValue();
        // Do something with $value
    }
}

This example shows the basic process for reading an XLSX file using PHPExcel. You can modify the code to suit your specific needs. Note that PHPExcel is no longer actively maintained and has been replaced by PHPSpreadsheet, which offers similar features and better compatibility with newer versions of PHP and Excel.

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.

Leave a Comment