如何使用 PHP 代码从 excel 文件中读取特定行以显示有关 html 输出的信息?

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

如何使用 PHP 代码从 excel 文件中读取特定行以显示有关 html 输出的信息?

<?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>

如果值存在,此函数将 return 用户提供的任何行作为输入。

// 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";
  }

}

希望对您有所帮助:)