Sujini R Sujini R - 1 month ago 7
PHP Question

How to display only title field name in xl file using php

Here i want to do read the xl file in php,here i displayed all datas that means in xl file i have four columns called Title,Url,Visitors,Accesses.but for me don't want like this , i want only title name how can do this ? View my answer



<?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('test.xls');

function sheetData($sheet) {
$re = '<table>'; // starts html table
$row = 1;
while($row <= $sheet['numRows']) {
$re .= "<tr>\n";
$column = 1;
$cell = isset($sheet['cells'][$row][1]) ? $sheet['cells'][$row][1] : '';
$re .= " <td>$cell</td>\n";
$re .= "</tr>\n";
$row++;
}
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/>';
$excel_data .= 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;
?>

</body>
</html>




Answer

By observing the $sheet array you will get Title at the position of 4,1 as follows.

By changing this line as shown

 $cell = isset($sheet['cells'][4][1]) ? $sheet['cells'][4][1] : '';

But it looks like you have copied this code from somewhere. It is hard to identify your need and modify it. Change the code as per your requirement and if any error occurs then post your question on SO

as per your requirement your sheetData function should be like this

function sheetData($sheet) {
  $re = '<table>';     // starts html table

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

     if($cell != 'Title'){  // check for title here
        $re .= " <td>$cell</td>\n";  
        $re .= "</tr>\n"; 
      }
        $x++;

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