PHPExcel search/return 单元格中包含特定值的所有行

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;\">&nbsp;'.$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;

逻辑: 我只是将该行存储到临时变量中,并将行的每个单元格值与所需内容进行比较。如果它匹配,那么我将 $contentFound 设置为 ture,并将临时行添加到主 table ($table var)。在下一个垂直阅读循环中,$tempRow and $contentFound 将设置为原始状态,因此最终需要的行将在我们的输出中。

注意:据我所知,PHPExcel 中没有内置搜索功能。