Загрузить данные Excel на webiste через phpspreadsheet - PullRequest
0 голосов
/ 23 марта 2020

С помощью PHPSpreadreader я хочу загрузить файл Excel на свой веб-сайт (в таблицу HTML). Инструкции: https://phppot.com/php/import-excel-file-into-mysql-database-using-php/

Пока все работает, но когда я хочу загрузить файл Excel, содержащий более 10 столбцов, код больше не работает. Я изменил код соответствующим образом (от 0 до 11 строк).

У кого-нибудь есть идеи, что может быть причиной?

<h2>Import Excel File into MySQL Database using PHP</h2>
    
    <div class="outer-container">
        <form action="" method="post"
            name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">
            <div>
                <label>Choose Excel
                    File</label> <input type="file" name="file"
                    id="file" accept=".xls,.xlsx">
                <button type="submit" id="submit" name="import"
                    class="btn-submit">Import</button>
        
            </div>
        
    </form>
    </div>
    <div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
         
<?php
    $sqlSelect = "SELECT * FROM tbl_info";
    $result = mysqli_query($conn, $sqlSelect);

if (mysqli_num_rows($result) > 0)
{
?>
        
    <table class='tutorial-table'>
        <thead>
            <tr>
                <th>Name</th>
                <th>Name1</th>
                <th>Name2</th>
                <th>Name3</th>
                <th>Name4</th>
                <th>Name5</th>
                <th>Name6</th>
                <th>Name7</th>
                <th>Name8</th>
                <th>Name9</th>
                <th>Name10</th>
                <th>Name11</th>
            </tr>
        </thead>
<?php
    while ($row = mysqli_fetch_array($result)) {
?>                  
        <tbody>
        <tr>
<td><?php  echo $row['name']; ?></td>
<td><?php  echo $row['name1']; ?></td>
<td><?php  echo $row['name2']; ?></td>
<td><?php  echo $row['name3']; ?></td>
<td><?php  echo $row['name4']; ?></td>
<td><?php  echo $row['name5']; ?></td>
<td><?php  echo $row['name6']; ?></td>
<td><?php  echo $row['name7']; ?></td>
<td><?php  echo $row['name8']; ?></td>
<td><?php  echo $row['name9']; ?></td>
<td><?php  echo $row['name10']; ?></td>
<td><?php  echo $row['name11']; ?></td>
        </tr>
<?php
    }
?>
        </tbody>
    </table>
<?php 
} 
?>
<?php
$conn = mysqli_connect("localhost","root","test","phpsamples");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');

if (isset($_POST["import"]))
{
       
  $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  
  if(in_array($_FILES["file"]["type"],$allowedFileType)){

        $targetPath = 'uploads/'.$_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
        
        $Reader = new SpreadsheetReader($targetPath);
        
        $sheetCount = count($Reader->sheets());
        
        for($i=0;$i<$sheetCount;$i++)
        {
            $Reader->ChangeSheet($i);
            
            foreach ($Reader as $Row)
            {
          
                $name = "";
                if(isset($Row[0])) {
                    $name = mysqli_real_escape_string($conn,$Row[0]);
                }
                
                $name1 = "";
                if(isset($Row[1])) {
                    $name1 = mysqli_real_escape_string($conn,$Row[1]);
                }
                
                  $name2 = "";
                if(isset($Row[2])) {
                    $name2 = mysqli_real_escape_string($conn,$Row[2]);
                }
                
                $name3 = "";
                if(isset($Row[3])) {
                    $name3 = mysqli_real_escape_string($conn,$Row[3]);
                }
                
                  $name4= "";
                if(isset($Row[4])) {
                    $name4 = mysqli_real_escape_string($conn,$Row[4]);
                }
                
                $name5 = "";
                if(isset($Row[5])) {
                    $name5 = mysqli_real_escape_string($conn,$Row[5]);
                }
                
                  $name6 = "";
                if(isset($Row[6])) {
                    $name6 = mysqli_real_escape_string($conn,$Row[6]);
                }
                
                $name7 = "";
                if(isset($Row[7])) {
                    $dname7 = mysqli_real_escape_string($conn,$Row[7]);
                }
                
                  $name8 = "";
                if(isset($Row[8])) {
                    $name8 = mysqli_real_escape_string($conn,$Row[8]);
                }
                
                $name9 = "";
                if(isset($Row[9])) {
                    $name9 = mysqli_real_escape_string($conn,$Row[9]);
                }
                
                  $name10 = "";
                if(isset($Row[10])) {
                    $name10 = mysqli_real_escape_string($conn,$Row[10]);
                }
                
                $name11  "";
                if(isset($Row[11])) {
                    $name11 = mysqli_real_escape_string($conn,$Row[11]);
                }
                
                if if (!empty($name) || !empty($name1) || !empty($name2) || !empty($name3) || !empty($name4) || !empty($name5) || !empty($name6) || !empty($name7) || !empty($name8) || !empty($name9) || !empty($name10) || !empty($name11)) {
                    $query = "insert into tbl_info (name,name1,name2,name3,name4,name5,name6,name7,name8,name9,name10,name11) values('".$name."','".$name1."','".$name2."','".$name3."','".$name4."','".$name5."','".$name6."','".$name7."','".$name8."','".$name9."','".$name10."','".$name11.")";
                    $result = mysqli_query($conn, $query);
                
                    if (! empty($result)) {
                        $type = "success";
                        $message = "Excel Data Imported into the Database";
                    } else {
                        $type = "error";
                        $message = "Problem in Importing Excel Data";
                    }
                }
             }
        
         }
  }
  else
  { 
        $type = "error";
        $message = "Invalid File Type. Upload Excel File.";
  }
}
?>
...