从 excel 检索数据并显示在页面中

retrieving data from excel and showing in the page

大家好,我有一个 excel sheet,我想提取一些信息并发布... 看起来像

到目前为止,我已经将它们放入公司名称选项卡中,例如下拉菜单。

我想做如图的选中信息:

但我无法使其动态化以使其显示....您能否提供有关如何使其动态化的线索?(不是询问 ajax...我要求从从数组中选择并显示可用的) 这是代码

<?php
    set_include_path(implode(PATH_SEPARATOR, [
        realpath(__DIR__ . '/Classes'), // assuming Classes is in the same directory as this script
        get_include_path()
    ]));
    require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
    require_once 'PHPExcel.php';
    $file= "./uploads/".$_GET["filename"];
    $inputFileName = ($file);
    //  Read your Excel workbook
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } 
    catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }
    //  Get worksheet dimensions
    $sheet = $objPHPExcel->getSheet(0); 
    $highestRow = $sheet->getHighestRow(); 
    $highestColumn = $sheet->getHighestColumn();

    /*
    $total=array();
    //  Loop through each row of the worksheet in turn

    for ($row = 1; $row <= $highestRow; $row++) { 

    //  Read a row of data into an array
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
    //  echo "-----------------as rowData---------------";


    //  var_dump($rowData); //  Insert row data array into your database of choice here
    //    echo "-----------------VAR_DUMP total!---------------";


        array_push($total, $rowData);
    //  var_dump($total);
        $myFlatArray = PHPExcel_Calculation_Functions::flattenArray($total);    
        echo    "<br>";
        echo "----------------- total as json encode---------------";
        var_dump(json_encode($myFlatArray));
    }
    */
    $foundInCells = array();
    $searchValue = 'Company';

    $wscounter=0; //worksheet counter
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        echo "<hr><p> Worksheet count: " . $wscounter . " name: " . $worksheet->getTitle() . "</p>";
        ///START Sirket isimlerini listele




        $column = 'B';
        $lastRow = $worksheet->getHighestRow();
        echo '<p> Company name: <select id="my_select">';
        for ($row = 1; $row <= $lastRow; $row++) {

            if($worksheet->cellExists($column.$row))
                {
                echo "<option value='".$column.$row."'>". $worksheet->getCell($column.$row)->getValue() . "</option>";

            }
        }

        echo "</select> </p>";
        ///END Sirket isimlerini listele 

        $rowCompanyInfoStart = 4;
        $rowCompanyInfoEnd = 9;
        $colCompanyInfoStart = 'C';
        $colCompanyInfoEnd = 'L';
        echo "<table border='1'>";

//      echo " print row ";
//  echo $_POST["getCell($column.$row)"];


        for ($rowcount = $rowCompanyInfoStart; $rowcount <= $rowCompanyInfoEnd; $rowcount++) { 

            //$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
            $rangeCoordinates = $colCompanyInfoStart . $rowcount . ':' . $colCompanyInfoEnd . $rowcount;
            $rowData = $sheet->rangeToArray($rangeCoordinates, NULL, TRUE, FALSE);

            //fazla bosluk olursa bunları aç ya da hucre bos mu kontrol et (cellExists ile) 
            //rowData = array_map('array_filter', $rowData);
            //$rowData = array_filter($rowData);
            echo "<tr>";
            foreach($rowData[0] as $result) {
                echo "<td>".$result." </td>";
            }
            echo "</tr>";
        }
        echo "</table>";


    /* GB01 START

        //try {
        /// Y 0 değil 1 den basliyor dikkat
            $rowcounter=1;       
            foreach ($worksheet->getRowIterator() as $row) {
                echo "Worksheet " . $wscounter . " Row " . $rowcounter . "<br/>";
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(true);

                $cellcounter=0; 
                $hitcounter=0;
                foreach ($cellIterator as $cell) {
                    echo "Worksheet " . $wscounter . " Row " . $rowcounter . " Cell " . $cellcounter . "<br/>";
                    if ($cell->getValue() == $searchValue) {
                        $hitcounter++;
                        echo "hitcounter" . $hitcounter . "<br/>";
                        $foundInCells[] = $worksheet->getTitle() . '!' . $cell->getCoordinate();
                    }
                    $cellcounter++;
                }
                $rowcounter++;
            }
        /*
        }
        // catch (Exception $exc) {
        //   echo "<div style='padding:5px; background-color:#fc8888'> Exception caught on Worksheet " . $wscounter . " Row " . $rowcounter . "<br/>" . $exc->getMessage() . "</div>";
        // }
        $wscounter++;
        //GB01 END */   
    }
    echo "-----found in cells----------------------------------------------------------------";
    var_dump($foundInCells);

?>

<!doctype html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Autocomplete - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <script>
     /* $(function() {
        var availableTags = $.parseJSON('<?php echo json_encode($myFlatArray); ?>');
        $( "#tags" ).autocomplete({
        source: availableTags
        });
      });  */

    function todo()
    {

    var xmlhttp;
if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("my_select").innerHTML=xmlhttp.responseText;
    }
  }

    }



    </script>
    </head>
    <body>
        <br><br><br>
        <div class="ui-widget">
            <label for="tags">Tags: </label>
            <input id="tags">
            <br><br><br><br>
        </div>
    </body>
</html>

我不太了解 php 我知道这不会有效,但我仍然想解释它应该如何工作,

一个解决方案可能是将对象保存在数组中的下拉列表中,应该是这样的:

$array = new array();
<?php
  $record = array($id, $name, $row, $col);
  array_push($array, $record);
?>

那么我想您可以使用 $array[x][0] 填充您的下拉列表,使用 x 作为增量值,0 指的是 $id您现在有可用形式的键&行&列。

现在我们必须从 excel 中获取值。如果文件大小合理,您可以将它们保存在内存中,或者在每次更改时解析 excel 。 无论如何,您都需要数据单元格的坐标。

单击提交按钮(或该下拉菜单的选择更改事件)后,您可以获取值(id)字段。还记得 $array 吗?我们有数据的位置。

IF B2$name 变量,- [C4-L9] 是你的数据范围。

让我们以自动方式获取坐标:

/*these are just mathematical operations, i wrote just to show what you should look for*/
$col[n]+1 = C
$row[n]+2 = 4

$col[n+1]+1 = C
$row[n+1]-2 = 9

现在我们有了左上角和左下角坐标。如果您的数据字段数是固定的(在这种情况下为 10),只需将列数增加 10。使用您在代码中使用的 rangeToArray 函数,范围应指向左上角-右下角,比方说 $array2 .

您可以使用类似的 foreach 循环从 $array2 中打印出数据,而无需 ajax 我想这就是您所要求的。

这是我的完整解决方案!如果您有任何问题,请在此解决方案下发表评论,但效果非常好!

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Choose a Company</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
        <script>
        </script>           
    </head>
    <style>
        div.back {
        border:2px solid grey;
        margin:auto;
        position:relative;}
        .button {
        margin-bottom:200 px;
        }
    </style>
    <script>
    </script>
    <body>
        <div id="header">
            <h1><u> Choose a Company </u></h1>
        </div>
        <div class="back">
            <p>For going to the previous excel list, please press to "Back" button </p>
            <a href="http://localhost:81/phpex/list.php">
                <input type="button" class="button"  value=" Back " />
            </a>
        </div>
        <div id="action">
            <p>OR</p>
            <p>Please from "Company name" options choose a name then Press to "Get Selected Values" </p>
        </div>
    </body>
</html>
<?php
    require_once dirname(__FILE__) .'/tcpdf/tcpdf.php';
    set_include_path(implode(PATH_SEPARATOR, [
    realpath(__DIR__ . '/Classes'), // assuming Classes is in the same directory as this script
    get_include_path()
    ]));
    require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
    require_once 'PHPExcel.php';
    $file= "./uploads/".$_GET["filename"];
    $inputFileName = ($file);
    //  Read your Excel workbook
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } 
    catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }
    //  Get worksheet dimensions
    $sheet = $objPHPExcel->getSheet(0); 
    $highestRow = $sheet->getHighestRow(); 
    $highestColumn = $sheet->getHighestColumn();
    session_start();
//  $_SESSION["color"] = "green";
    $wscounter=0; //worksheet counter
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        //  echo "<hr><p> Worksheet count: " . $wscounter . " name: " . $worksheet->getTitle() . "</p>";
        ///START Sirket isimlerini listele
        $count=0;
        $column = 'C';
        $lastRow = $worksheet->getHighestRow();
        echo '<form action="#" method="post">';
        echo '<p> Company name: <select name="my_select">';
        for ($row = 1; $row <= $lastRow; $row++)
        {       
            if($worksheet->cellExists($column.$row))
            {
                echo "<option value='".$column.'.'.$row."'>". $worksheet->getCell($column.$row)->getValue() . "</option>";
            }
        }   
        echo "</select> </p>";
        echo "<br />";
        echo    '<input type="submit" name="submit" value="Get Selected Values" />';
        echo "</form>";
        echo "<br />";
        $comp = array(100);
        $tempcount=0;
        for ($row = 1; $row <= $lastRow; $row++)
        {   
            if($worksheet->cellExists($column.$row))
            {
                $count++;
                $name=  $worksheet->getCell($column.$row)->getValue();
                $comp[$tempcount] = array($name,$column,$row);
                //var_dump($comp[$tempcount]);
                //  echo "<p><b>rank number $row </b></p>";
                for ($col = 0; $col < 3; $col++)
                {
                    //  echo "<li>".$comp[$cnt][$col]."</li>";
                }
                $tempcount++;
                //  echo"sadece tempcount";
                //  var_dump($tempcount);
            }
        }
        //  echo "tempcount comp burda";
        //  var_dump ($comp[31]);
        //  echo $count;
        if(isset($_POST['submit']))
        {
            $selected_val = $_POST['my_select'];  // Storing Selected Value In Variable
            $only_row = explode('.',$selected_val);
            //echo "You have selected :" .$selected_val. "<br />";  // Displaying Selected Value
            //var_dump($only_row);
            //echo "selected row value :".$only_row[1]. "<br />";
            for($i=0; $i< $count; $i++)
            {
                if ($comp[$i][2]== $only_row[1])
                { 
                    $info_end=($comp[$i+1][2]-1);
                }
            }
        }   
        else{error_reporting(E_ALL ^ E_NOTICE);}    
        $rowCompanyInfoStart = $only_row[1]+2;
        $rowCompanyInfoEnd = $info_end;
        $colCompanyInfoStart = 'D';
        $colCompanyInfoEnd = 'N';



        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            echo '<form action="final.php" method="POST">';
            $tablo="<br />";
            $tablo = $tablo."<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\" align='center'>";
            for ($rowcount = $rowCompanyInfoStart; $rowcount <= $rowCompanyInfoEnd; $rowcount++)
            { 
                //$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
                $rangeCoordinates = $colCompanyInfoStart . $rowcount . ':' . $colCompanyInfoEnd . $rowcount;
                $rowData = $sheet->rangeToArray($rangeCoordinates, NULL, TRUE, FALSE);
                //fazla bosluk olursa bunları aç ya da hucre bos mu kontrol et (cellExists ile) 
                //rowData = array_map('array_filter', $rowData);
                //$rowData = array_filter($rowData);    
                $tablo= $tablo."<tr >";
                $companyname=$worksheet->getCell($column.$row)->getValue();
                //  echo $companyname;              
                foreach($rowData[0] as $result) 
                {
                    $tablo= $tablo. "<td>".$result. " </td>";
                }
                $tablo= $tablo. "</tr>";
            }
            $tablo= $tablo. "</table>";
            echo $tablo;
            echo "<br />";

            if($_SERVER['REQUEST_METHOD'] != 'POST') {
                echo "SESSION Not AVAIL. first run.";
            }


            else{   
                $_SESSION['varname'] = $tablo;
                }

            echo    '<input type="submit" name="resName" value="Convert into PDF" />';

        }
        echo '</form>';
        echo "<br />";
        echo "<br />";
    }
?>