我如何在CakePHP 4中解析和读取Excel文件?

z9smfwbn  于 2022-11-12  发布在  PHP
关注(0)|答案(1)|浏览(161)

我想读取我的Excel文件,这是xlsx。有一些错误来了。我能做什么?这是cakephp 4。我用SimpleXLSX类。这是我的索引PHP文件。index.php

<h2>Upload form</h2>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="file"  />
    <input type="submit" value="Parse" />
</form>

<?php

//https://github.com/shuchkin/simplexlsx/blob/master/src/SimpleXLSX.php THIS CLASS I USE THERE

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

echo '<h1>XLSX to HTML</h1>';

if (isset($_FILES['file'])) {

    if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {

        echo '<h2>Parsing Result</h2>';
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';

        $dim = $xlsx->dimension();
        $cols = $dim[0];

        foreach ( $xlsx->rows() as $k => $r ) {
            echo '<tr>';
            for ( $i = 0; $i < $cols; $i ++ ) {
                echo '<td>' . ( isset( $r[ $i ] ) ? $r[ $i ] : '&nbsp;' ) . '</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    } else {
        echo SimpleXLSX::parseError();
    }
}

控制器中没有要写入的内容。
excelController.php

<?php

    namespace App\Controller;

    class excelController extends AppController
    {
        /**
         * Index method
         *
         * @return \Cake\Http\Response|null|void Renders view
         */
        public function index()
        {

        }
    }
6qqygrtg

6qqygrtg1#

“”〈?php
回声

剖析Excel数据

“; echo”“;

$i = 0;

foreach ($xlsx->rows() as $elt) {

if ($i == 0) {
    echo "<tr><th scope='col'>" . $elt[0] . "</th><th scope='col'>" . $elt[1] . "</th><th scope='col'>" . $elt[2] . "</th><th scope='col'>" . $elt[3] . "</th><th scope='col'>" . $elt[4] . "</th><th scope='col'>" . 'Remark' . "</th></tr>";
} 
else 
{
    echo "<tbody>";

    if(gettype($elt[0]) == 'integer' && gettype($elt[1]) == 'integer' && gettype($elt[2]) == 'integer' && gettype($elt[3]) == 'integer' && gettype($elt[4]) == 'integer'){
        echo "<tr><td>" . $elt[0] . "</td><td>" . $elt[1] . "</td><td>" . $elt[2] . "</td><td>" . $elt[3] . "</td><td>" . $elt[4] . "</td><td>" . '<button type="button" class="btn btn-primary">Candidate List</button><button type="button" class="btn btn-light">Candidate List(Examiner)</button>' . "</td></tr>";
    }
    else{
        if(gettype($elt[0]) == 'string'){
            echo "<tr class='danger'><td style='color: red;'>" . '<h4><del>'.$elt[0].'</del></h4>' . "</td><td>" . $elt[1] . "</td><td>" . $elt[2] . "</td><td>" . $elt[3] . "</td><td>" . $elt[4] . "</td><td>" . '<h6 class="text-center">Please Input the Correct Data.</h5>' . "</td></tr>";
        }
        else if(gettype($elt[1]) == 'string'){
            echo "<tr class='danger'><td>" . $elt[0] . "</td><td style='color: red;'>" . '<h4><del>'.$elt[1].'</del></h4>' . "</td><td>" . $elt[2] . "</td><td>" . $elt[3] . "</td><td>" . $elt[4] . "</td><td>" . '<h6 class="text-center">Please Input the Correct Data.</h5>' . "</td></tr>";
        }
        else if(gettype($elt[2]) == 'string'){
            echo "<tr class='danger'><td>" . $elt[0] . "</td><td>" . $elt[1] . "</td><td style='color: red;'>" . '<h4><del>'.$elt[2].'</del></h4>' . "</td><td>" . $elt[3] . "</td><td>" . $elt[4] . "</td><td>" . '<h6 class="text-center">Please Input the Correct Data.</h5>' . "</td></tr>";
        }
        else if(gettype($elt[3]) == 'string'){
            echo "<tr class='danger'><td>" . $elt[0] . "</td><td>" . $elt[1] . "</td><td>" . $elt[2] . "</td><td style='color: red;'>" . '<h4><del>'.$elt[3].'</del></h4>' . "</td><td>" . $elt[4] . "</td><td>" . '<h6 class="text-center">Please Input the Correct Data.</h5>' . "</td></tr>";
        }
        else if(gettype($elt[4]) == 'string'){
            echo "<tr class='danger'><td>" . $elt[0] . "</td><td>" . $elt[1] . "</td><td>" . $elt[2] . "</td><td>".$elt[3]."</td><td style='color: red;'>" . '<h4><del>'.$elt[4].'</del></h4>' . "</td><td>" . '<h6 class="text-center">Please Input the Correct Data.</h5>' . "</td></tr>";
        }

    }

}      

$i++;

}
回显“";
?〉'''
在视图中使用此代码,并对控制器“”$data = $this-〉request-〉data;如果($data ['file']){

if ( $xlsx = SimpleXLSX::parse( $data['file']['tmp_name'] ) ) {
            $dim = $xlsx->dimension();
            $cols = $dim[0];

            $this->set(compact('xlsx', 'dim', 'cols'));
        } else {
            echo SimpleXLSX::parseError();
        }
    }'''

相关问题