我如何在 CakePHP 4 中解析和读取 excel 文件?
How can i parse and read the excel file in CakePHP 4?
我想阅读我的 excel 文件,它是 xlsx 格式的。出现一些错误。我能做什么?这是 cakephp 4。我在那里使用 SimpleXLSX class。有我的索引 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 ] : ' ' ) . '</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()
{
}
}
'''
echo '
正在解析Excel数据
';
回声 '';
$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;
如果($数据['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();
}
}'''
我想阅读我的 excel 文件,它是 xlsx 格式的。出现一些错误。我能做什么?这是 cakephp 4。我在那里使用 SimpleXLSX class。有我的索引 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 ] : ' ' ) . '</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()
{
}
}
'''
echo '
正在解析Excel数据
'; 回声 '';$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; 如果($数据['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();
}
}'''