DEV Community

programmer desk
programmer desk

Posted on

Read XLXS file in PHP and display header.

Read XLXS file using PHP

  • Use lib from GitHub call XLSXReader XLSXReader will helps to achieve this as it provide all the require functions. so, it begin with call it and use it further.

After install Composer and XLSXREADER call below file at top.

require('./XLSXReader.php');

Enter fullscreen mode Exit fullscreen mode
  • Use any ExcelSheet for example..
    Add any Excel file in root of the project directory. So, we can use it latter.

  • Create index.php file and add below 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;
Enter fullscreen mode Exit fullscreen mode

Run it on your local or server. For more about demo and output visit at programmerdesk

Top comments (0)