How to read xlxs file and store in DB

In this tutorial we are going to implement read xlxs file in php and display data by sheet name. We use XLSXReader lib to achieve this, So get if from github first and setup it on local.

Steps of the file execution below:

  • Get XLSXReader lib and Include this from top of the file
  • Use any excel file for read or you can add import code to execute it.
  • Add code for read data and display it by table.
require('./XLSXReader.php');
read xlxs file


We use any sheet for our example and we plan to execute those each row data and display it by table. below is the complete source code.

<?php
require('./XLSXReader.php');

$targetPath = './hello world.xlsx';
$xlsx = new XLSXReader($targetPath);
$sheetNames = $xlsx->getSheetNames();

$imports=array();
$html="";
foreach($sheetNames as $sheetName) {
	
	$sheet = $xlsx->getSheet($sheetName);
	echo "<h3>".$sheet->sheetName."</h3>";
	
	$html.="<table border='1'>";
	foreach($sheet->getData() as $row) {
		$html.="<tr>";
		foreach($row as $key){
			$html.="<td>".$key."</td>";	
		}
		$html.="<tr>";	
	}
	$html.="</table>";
}
echo $html;

Output

read xlxs file