2015-04-14 103 views
1

我想搜索一個字符串,將它與數組進行比較,並輸出 單元格及其對應的行。帶搜索按鈕的PHPExcel

在此先感謝!

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    </head> 
    <body> 
     <form action="index.php" class="contact_form" method = "post"> 
      <p>Please enter your name:</p> 
      <input name="fname" id="fname" type="fname"/><br/> 
      <br> 
      <br> 
      <input type="submit" name="submit" class="submit" value="Submit" /> 
     </form> 

<?php 
    /** Include path **/ 
    set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); 

    /** PHPExcel_IOFactory */ 
    include 'PHPExcel/IOFactory.php'; 


    $inputFileName = './check.xlsx'; // File to read 
    //echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />'; 
    try { 
     $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
    } catch(Exception $e) { 
     die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());  
    } 

    echo '<hr />'; 
    echo "<pre>"; 
    $data = array(); 
    $worksheet = $objPHPExcel->getActiveSheet(); 

    foreach ($worksheet->getRowIterator() as $row) { 
     $cellIterator = $row->getCellIterator(); 
     $cellIterator->setIterateOnlyExistingCells(false); 

     foreach ($cellIterator as $cell_raw) { 
      $data[$cell_raw->getRow()][$cell_raw->getColumn()] = $cell_raw->getValue(); 
      $c =$_POST['fname']; 

      if ($c === $data){ 
       echo 'test'; 
      } 
     } 
    } 

    var_dump($data); 

/* foreach($sheetData as $rec) { 
    print_r($rec); 
} 
*/ 

?> 
    <body> 
</html> 

我想搜索,它比較所述陣列和輸出 細胞和其對應的行的字符串。

回答

0

保存-的index.php

<form action="2.php" class="contact_form" method = "post"> 
    <p>Please enter your name:</p> 
    <input name="fname" id="fname" type="fname"/><br/> 
    <br> 
    <br> 
    <input type="submit" name="submit" class="submit" value="Submit" /> 
</form> 
+0

請說爲什麼你的答案是對問題的解決方案。 – Koopakiller

0

保存2.PHP

<html> 
 
    <head> 
 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 
    </head> 
 
    <body> 
 
     
 

 
<?php 
 
    /** Include path **/ 
 
    set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); 
 

 
    /** PHPExcel_IOFactory */ 
 
    include 'PHPExcel/IOFactory.php'; 
 

 

 
    $inputFileName = './jd.xlsx'; // File to read 
 
    //echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />'; 
 
    try { 
 
     $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
 
    } catch(Exception $e) { 
 
     die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());  
 
    } 
 

 
    echo '<hr />'; 
 
    echo "<pre>"; 
 
    $data = array(); 
 
    $worksheet = $objPHPExcel->getActiveSheet(); 
 

 
    foreach ($worksheet->getRowIterator() as $row) { 
 
     $cellIterator = $row->getCellIterator(); 
 
     $cellIterator->setIterateOnlyExistingCells(false); 
 

 
     foreach ($cellIterator as $cell_raw) { 
 
      $data[$cell_raw->getRow()][$cell_raw->getColumn()] = $cell_raw->getValue(); 
 
      $c =$_POST['fname']; 
 
echo "</br><b>"; 
 
echo $cell_raw; 
 
echo "</br></b>"; 
 

 
$strsearch = (strpos($cell_raw, $c) !== FALSE); 
 
if($strsearch){ 
 

 
    echo "<b> Results found for " .$c."</b>"; 
 

 
} 
 

 
else{ 
 
    echo "No results found for $c"; 
 
} 
 

 
      if ($c == $cell_raw){ 
 
\t \t \t 
 
echo "</b></br>"; 
 
       echo '<b>if check - Match </b> '; 
 
      } 
 
\t \t \t else{ 
 
\t \t \t 
 
echo "</b></br>"; 
 
\t \t \t echo "if check - 0"; } 
 
\t \t \t echo "</br>"; 
 
\t \t \t \t $search =array($cell_raw); 
 
if (in_array($c, $search)) { 
 
    echo "<b>array check - Got $c </b>"; 
 
} 
 
else { 
 
    echo "array check- No results - $c"; 
 
} 
 
\t echo "</br>"; \t \t 
 
     } 
 
    } 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 

 
echo "</br>"; \t 
 
\t  echo "var_dump"; 
 
echo "</br>"; 
 
     var_dump($data); 
 
\t 
 
echo "</br>"; \t 
 
     echo "print_r"; \t 
 
echo "</br>"; 
 
\t  print_r ($data); 
 

 
/* foreach($sheetData as $rec) { 
 
    print_r($rec); 
 
} 
 
*/ 
 
    
 

 
?> 
 

 
    <body> 
 
</html>

+0

我不確定人們爲什麼不寫'
'而不是'
'。 –