doucong6884 2018-07-31 02:25
浏览 97
已采纳

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

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>
";
    $y = 1;
    while($y <= $sheet['numCols']) {
      $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';
      $re .= " <td>$cell</td>
";  
      $y++;
    }  
    $re .= "</tr>
";
    $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条回答 默认 最新

  • ds0409 2018-07-31 04:40
    关注

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

    Hope it helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值