使用php在sql中导入excel

vuktfyat  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(297)

代码:

{
<?php
$connect = mysqli_connect("localhost", "root", "", "refe_book");
$output = '';
if(isset($_POST["import"]))
{
$extension = end(explode(".", $_FILES["excel"]["name"])); // For getting 
Extension of selected file
$allowed_extension = array("xls", "xlsx", "csv"); //allowed extension
if(in_array($extension, $allowed_extension)) //check selected file extension 
is present in allowed extension array
{
$file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file
include("PHPExcel/Classes\PHPExcel\IOFactory.php"); // Add PHPExcel Library in this code
$objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel 
library by using load() method and in load method define path of selected file

$output .= "<label class='text-success'>Data Inserted</label><br /><table 
class='table table-bordered'>";
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$ins_flag = 0;
$company_name = "";
for($row=1; $row<=$highestRow; $row++)
{
if ($ins_flag == 1)
{
$output .= "<tr>";
$alias = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
$company_name = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
$product_name = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
$unit = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
$mrp = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(3, $row)->getValue());

if ($product_name[$row][1] == null)
{
$company_name = $alias[$row][0];
}
else if ($product_name[$row][1] != null)
{
$query = "INSERT INTO refe_book_table(alias, company_name, product_name, unit, mrp) VALUES ('".$alias."', '".$company_name."', '".$product_name."', '".$unit."', '".$mrp."')";
        mysqli_query($connect, $query);
        $output .= '<td>'.$alias.'</td>';
        $output .= '<td>'.$company_name.'</td>';
        $output .= '<td>'.$product_name.'</td>';
        $output .= '<td>'.$unit.'</td>';
        $output .= '<td>'.$mrp.'</td>';
        $output .= '</tr>';
     }
   }

}
} 
$output .= '</table>';

}
else
{
$output = '<label class="text-danger">Invalid File</label>'; //if non excel file then
}
}
?>

<html>
 <head>
  <title>Import Excel to Mysql using PHPExcel in PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> 
</script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <style>
  body
  {
   margin:0;
   padding:0;
   background-color:#f1f1f1;
  }
  .box
  {
  width:700px;
  border:1px solid #ccc;
  background-color:#fff;
  border-radius:5px;
  margin-top:100px;
 }

 </style>
</head>
<body>
 <div class="container box">
  <h3 align="center">Import Excel to Mysql using PHPExcel in PHP</h3><br />
  <form method="post" enctype="multipart/form-data">
   <label>Select Excel File</label>
   <input type="file" name="excel" />
   <br />
   <input type="submit" name="import" class="btn btn-info" value="Import" />
  </form>
  <br />
  <br />
  <?php
  echo $output;
  ?>
 </div>
</body>
</html>
}

错误:
注意:在c:\xampp\htdocs\referencebook\index.php的第6行中,只有变量应该通过引用传递错误:不导入数据
源文件:

导入后,希望在mysql中导入如下图像:

在上面的代码中,导入了excel文件,并且数据应该像sql导入图像一样排序。数据不在mysql中导入,也不在浏览器中显示

u7up0aaq

u7up0aaq1#

除了一个变量外,不能向 end() ,您正在向其中传递一个函数,但php内部需要一个变量。
改变你的 end() 以下几点应该有用

$fileName = explode(".", $_FILES["excel"]["name"]);
$extension = end($fileName);

相关问题