这里我想在php中读取xl文件,这里我显示了所有数据,这意味着在xl文件中我有四列,称为标题,Url,访问者,访问。但是对于我来说不想这样,我只想要标题名怎么能做到呢?查看我的答案
<?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>
通过观察$sheet
数组,您将获得位于4,1位置的Title,如下所示。
修改这一行,如下所示
$cell = isset($sheet['cells'][4][1]) ? $sheet['cells'][4][1] : '';
但是看起来你从某个地方复制了这段代码。很难确定你的需求并加以修改。根据您的要求更改代码,如果发生任何错误,然后在SO
上发布您的问题。根据您的要求,您的sheetData
功能应该是这样的
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
}