PHPExcel搜索/返回单元格中包含特定值的所有行
PHPExcel search/return all rows containing perticular value in cells
我试图找到一种方法,只使用PHPExcel获取特定的行。在网上搜索后,我无法独自找到答案。我想做的是列出所有具有"I1/027"作为列内容的行。
例如:
Hours | Place | Name
------|-------|-----------------
3 |I1/027 | example1 //------> Want to add it to my list!!!
6 |I2/025 | example2 //------> Ignore this
7 |I1/030 | example3
2 |I1/027 | example4 //------> Want to add it to my list!!!
这是没有行过滤器的代码,它显示了所有excel行:
<?php if(isset($_FILES['file']['name'])) { ?>
<!-- Container progress bar -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- progress info -->
<div id="information" style="width"></div>
<?php require_once 'reader/Classes/PHPExcel/IOFactory.php';
//Extra functions
function get_cell($cell, $objPHPExcel){
//Cell selection
$objCell = ($objPHPExcel->getActiveSheet()->getCell($cell));
//taking cell value
return $objCell->getvalue();
}
function pp(&$var){
$var = chr(ord($var)+1);
return true;
}
//==========Displaying Code
$name = $_FILES['file']['name'];
$tname = $_FILES['file']['tmp_name'];
$type = $_FILES['file']['type'];
if($type == 'application/vnd.ms-excel')
{ // excel 97 extension
$ext = 'xls';
}
else if($type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
{ // excel 2007 and 2010 extensions
$ext = 'xlsx';
}else
{ // invalid extension
echo -1;
exit();
}
$xlsx = 'Excel2007';
$xls = 'Excel5';
//read creator
$objReader = PHPExcel_IOFactory::createReader($$ext);
//loading
$objPHPExcel = $objReader->load($tname);
$dim = $objPHPExcel->getActiveSheet()->calculateWorksheetDimension();
// put $start and $end array
list($start, $end) = explode(':', $dim);
if(!preg_match('#([A-Z]+)([0-9]+)#', $start, $rslt)){
return false;
}
list($start, $start_h, $start_v) = $rslt;
if(!preg_match('#([A-Z]+)([0-9]+)#', $end, $rslt)){
return false;
}
list($end, $end_h, $end_v) = $rslt;
//starting to read excel doc
$table = "<table class='tabla'>";
for($v=$start_v; $v<=$end_v; $v++){
// calculate progress bar
$percent = intval($v/$end_v * 100)."%";
// progress bar update
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style='"width:'.$percent.';background-color:#ddd;'"> '.$percent.'</div>";
document.getElementById("information").innerHTML="'.$v.' files processades.";</script>';
// buffer flush
echo str_repeat(' ',1024*64);
// send exit to navigator
flush();
sleep(0.25);
//horizontal reading
$table .= "<tr>";
for($h=$start_h; ord($h)<=ord($end_h); pp($h)){
$cellValue = get_cell($h.$v, $objPHPExcel);
$table .= "<td>";
if($cellValue !== null){
$table .= $cellValue;
}
$table .= "</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
// process completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Procés complet"</script><br>';
echo $table;
}?>
</div>
提前感谢!!
解决方案:这里只需要使用简单的逻辑。更改您的水平阅读代码如下:
//horizontal reading
$tempRow= "<tr>";
$contentFound=false;
for($h=$start_h; ord($h)<=ord($end_h); pp($h)){
$cellValue = get_cell($h.$v, $objPHPExcel);
$tempRow.= "<td>";
if($cellValue !== null){
if($cellValue=="I1/027") $contentFound=true;
$tempRow.= $cellValue;
}
$tempRow.= "</td>";
}
$tempRow.= "</tr>";
if($contentFound) $table.=$tempRow;
逻辑:我刚刚将行存储到temp-var中,并将行的每个单元格值与所需内容进行比较。如果匹配,则我将$contentFound
设置为ture
,并将temp行添加到主表($table
var)。在下一个垂直读取循环中,$tempRow and $contentFound
将被设置为原始状态,所以最终所需的行将出现在我们的输出中。
注意:据我所知,PHPExcel中没有内置搜索功能。
相关文章:
- Javascript搜索和替换需要包含更多内容
- 搜索包含值的json对象键,然后取消设置
- td:包含变量中的搜索字符串
- 搜索包含包含所需值的范围的数组
- 如何存储推特搜索API推文并转发它们
- 创建一个CSS下拉菜单,其中包含从搜索图标弹出的搜索框
- 用于Cordova应用程序的Javascript SQLite-使用参数搜索,其中字符串包含子字符串大小写insenst
- 如何使用javascript/jquery/c#搜索和选择包含特殊字符的文本
- 在包含多个单词的表中搜索
- PHPExcel搜索/返回单元格中包含特定值的所有行
- 如何更新单词列表中搜索框中包含用户键入内容的单词/字母数
- 在引导选择组件中的搜索不起作用,其中数据令牌包含所有数字;4730”;
- jQuery/Datatables:在基本DataTable中包含许多不同的搜索过滤器类型和字段
- 来自用户的推特 API 高级搜索推文
- 如何通过 jquery 通配符搜索数据属性的精确包含值
- 如果当前网址不包含位置搜索,请重定向到主页
- 是否可以使用客户端脚本获取推特搜索 API 版本 1.1 的数据
- 如何计算某一列中包含JavaScript搜索词的所有TD
- 推特搜索 api 直到和自参数
- 如何使用正则表达式匹配不包含特定子字符串的字符串?