2017-09-15 68 views
0

我想上傳excel .xls文件,但當我試圖導入自動加載文件我的網頁變空白,當我評論它的作品時出現錯誤。我無法導入讀取器的噴口擴展文件。這裏是我的代碼。excel文件上傳到數據庫使用Php

use Box\Spout\Reader\ReaderFactory; 
use Box\Spout\Common\Type; 

require_once 'http://localhost/muddy/admin/spout-2.7.2/src/Spout/Autoloader/autoload.php';//Error cant import 

在這裏需要一次不能上傳文件,如果我寫這個代碼我的網頁空白!

if (!empty($_FILES['file']['name'])) { 
    echo "ks"; 
    $pathinfo = pathinfo($_FILES["file"]["name"]); 

    if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls') 
     && $_FILES['file']['size'] > 0) { 
     $inputFileName = $_FILES['file']['tmp_name']; 
     // Read excel file by using ReadFactory object. 
     $reader = ReaderFactory::create(Type::XLSX); 
     // Open file 
     $reader->open($inputFileName); 
     $count = 1; 

     foreach ($reader->getSheetIterator() as $sheet) { 
      echo "ks22"; 
      // Number of Rows in Excel sheet 
      foreach ($sheet->getRowIterator() as $row) { 
       echo "ks32"; 
       // It reads data after header. In the my excel sheet, 
       // header is in the first row. 
       if ($count > 1) { 
        echo "ks4"; 
        // Data of excel sheet 
        $data['Member_no'] = $row[0]; 
        $data['Member_name'] = $row[1]; 
        $data['Gender'] = $row[2]; 
        $data['Club_name'] = $row[3]; 
        $data['member_since'] = $row[4]; 
        $data['Expiry_date'] = $row[3]; 

        $member_no = $data['Member_no']; 
        $member_name = $data['Member_name']; 
        $gender = $data['Gender']; 
        $club_name = $data['Club_name']; 
        $member_since = $data['member_since']; 
        $expiry_date = $data['Expiry_date']; 

        $query="INSERT INTO `mmholdin_management`.`Club_member` (Member_no`, `Member_name`, `Gender`, `Club_name`, `member_since`, `Expiry_date`) VALUES ($member_no ,$member_name, $gender,$club_name,$member_since,$expiry_date)"; 
       echo $query; 
        if(mysql_query($query)) 
        { 
         $msg = "Record Saved!"; 
         //header("Location:managecustomer.php"); 
         exit; 
        } 
        else 
        { 
         $msg = "Unable to Save!"; 

        } 

        print_r(data); 

       } 
       $count++; 
      } 
     } 

     // Close excel file 
     $reader->close(); 

    } else { 

     echo "Please Select Valid Excel File"; 
    } 

} else { 

    //echo "Please Select Excel File"; 

} 
+0

爲什麼不能簡單地用'LOAD DATA INFILE'? – Rahul

+0

檢查此https://stackoverflow.com/questions/1310166/how-to-import-an-excel-file-in-to-a-mysql-database – Jenish

+0

csv代碼將能夠導入xls擴展名文件? @Jenish – kajal

回答

0

試試這個例子。

$file = "your-file.xls"; 
$handle = fopen($file, "r"); 
$c = 0; 
while(($filesop = fgetcsv($handle, 1000, ",")) !== false) 
{ 
$name = $filesop[0]; 
$email = $filesop[1]; 

$sql = mysql_query("INSERT INTO xls (name, email) VALUES ('$name','$email')"); 
} 

if($sql){ 
echo "You database has imported successfully"; 
}else{ 
echo "Sorry! There is some problem."; 
} 

檢查:https://www.studytutorial.in/how-to-upload-or-import-an-excel-file-into-mysql-database-using-spout-library-using-php

+0

This code Not Working!我有這個鏈接的代碼,但不工作! – kajal

+0

你得到的錯誤是什麼? – Sucharitha

+0

它不能從xsl文件讀取值。當我嘗試打印$名稱和它的空! – kajal

相關問題