R S Negi
R S Negi

Reputation: 13

how to read specific row from excel file to display information on html output using PHP code?

how to read specific row from excel file to display information on html output using PHP code?

<?php
include 'excel_reader.php';     // include the class

// creates an object instance of the class, and read the excel file data
$excel = new PhpExcelReader;
$excel->read('fees.xls');

// Excel file data is stored in $sheets property, an Array of worksheets
/*
The data is stored in 'cells' and the meta-data is stored in an array called 'cellsInfo'

Example (firt_sheet - index 0, second_sheet - index 1, ...):

$sheets[0]  -->  'cells'  -->  row --> column --> Interpreted value
         -->  'cellsInfo' --> row --> column --> 'type' (Can be 'date', 'number', or 'unknown')
                                            --> 'raw' (The raw data that Excel stores for that data cell)
*/

// this function creates and returns a HTML table with excel rows and columns data
// Parameter - array with excel worksheet data
function sheetData($sheet) {
  $re = '<table>';     // starts html table

  $x = 1;
  while($x <= $sheet['numRows']) {
    $re .= "<tr>\n";
    $y = 1;
    while($y <= $sheet['numCols']) {
      $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';
      $re .= " <td>$cell</td>\n";  
      $y++;
    }  
    $re .= "</tr>\n";
    $x++;
  }

  return $re .'</table>';     // ends and returns the html table
}

$nr_sheets = count($excel->sheets);       // gets the number of sheets
$excel_data = '';              // to store the the html tables with data of each sheet

// traverses the number of sheets and sets html table with each sheet data in $excel_data
for($i=0; $i<$nr_sheets; $i++) {
  $excel_data .= '<h4>Sheet '. ($i + 1) .' (<em>'. $excel->boundsheets[$i]['name'] .'</em>)</h4>'. sheetData($excel->sheets[$i]) .'<br/>';  
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Example PHP Excel Reader</title>
  <style type="text/css">
    table {
      border-collapse: collapse;
    }        
    td {
      border: 1px solid black;
      padding: 0 0.5em;
    }        
  </style>
</head>
<body>
  <?php
    // displays tables with excel file data
    echo $excel_data;enter code here
  ?>
</body>
</html>

Upvotes: 1

Views: 1993

Answers (1)

Nadun Kulatunge
Nadun Kulatunge

Reputation: 1731

This function will return any row the user give s as the input if values exist.

// this function creates and returns a HTML table with excel row given
// Parameter 1 - array with excel worksheet data
// Parameter 2 - any row you want to print
function sheetRowData($sheet,$rowNum) {
  $re = '<table>';     // starts html table

  $x = $rowNum; //get users Row Number as $x

  if($x <= $sheet['numRows']) {
    $re .= "<tr>\n";
    $y = 1;
    while($y <= $sheet['numCols']) {
      $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';
      $re .= " <td>$cell</td>\n";  
      $y++;
    }  
    $re .= "</tr>";
   return $re .'</table>';     // ends and returns the html table row
  }else{
    return "row not found";
  }

}

Hope it helps :)

Upvotes: 1

Related Questions