如何从Excel文件中读取特定行以使用PHP代码在html输出上显示信息?

时间:2018-07-31 02:25:17

标签: php phpexcel

如何使用Excel代码从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>

1 个答案:

答案 0 :(得分:0)

如果存在值,此函数将返回用户输入s的任何行。

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

}

希望它会有所帮助:)