溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫的方法

發(fā)布時間:2020-08-20 14:58:35 來源:億速云 閱讀:208 作者:小新 欄目:編程語言

這篇文章主要介紹了PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

 在這篇文章中,我將給大家介紹如何使用PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫。有時候我們需要從管理面板添加數(shù)據(jù),如產(chǎn)品,項目,用戶,電子郵件等。如果我們的數(shù)據(jù)很少,那么手工添加就可以了,但是如果我們的excel文件或者csv文件的數(shù)據(jù)比較多,那么存儲數(shù)據(jù)的時間就比較長,這時我們就需要直接導(dǎo)入xls文件或者csv文件到mysql數(shù)據(jù)庫中。

PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫的方法

下面我們將使用Spreadsheet_Excel_Reader類將excel文件導(dǎo)入php數(shù)據(jù)庫,步驟如下:

1.下載類庫

2.創(chuàng)建db_config.php文件

3.創(chuàng)建index . php文件

4.創(chuàng)建excelUpload.php

5.創(chuàng)建上傳文件夾

步驟1:下載類庫

從GitHub下載PHP Excel Reader庫,下載地址:https://github.com/nuovo/spreadsheet-reader

下載后將其解壓縮到根目錄并將其重命名為“library”。

步驟2:創(chuàng)建db_config.php文件

為數(shù)據(jù)庫配置創(chuàng)建db_config.php文件,在這個文件中,你必須設(shè)置數(shù)據(jù)庫主機、數(shù)據(jù)庫用戶名、數(shù)據(jù)庫密碼、數(shù)據(jù)庫名稱。該文件將用于將數(shù)據(jù)存儲到數(shù)據(jù)庫中。

代碼如下:

db_config.php

<?php
	$dbHost = "localhost";
	$dbDatabase = "h_php";
	$dbPasswrod = "root";
	$dbUser = "root";
	$mysqli = new mysqli($dbHost, $dbUser, $dbPasswrod, $dbDatabase);
?>

步驟3:創(chuàng)建index.php文件

在根目錄中創(chuàng)建index.php文件,在這個文件中,我使用bootstrap創(chuàng)建了一個簡單的表單,實現(xiàn)點擊按鈕后導(dǎo)入選擇excel文件的功能。

代碼如下:

index . php

<!DOCTYPE html>
<html>
<head>
        <meta charset="UTF-8">
	<title></title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
	<h2>Excel上傳</h2>
	<form method="POST" action="excelUpload.php" enctype="multipart/form-data">
		<div class="form-group">
			<label>上傳Excel文件</label>
			<input type="file" name="file" class="form-control">
		</div>
		<div class="form-group">
			<button type="submit" name="Submit" class="btn btn-success">上傳</button>
		</div>
	</form>
</div>


</body>
</html>

前臺樣式如下:

PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫的方法

步驟4:創(chuàng)建excelUpload.php文件

創(chuàng)建excelUpload.php文件來管理導(dǎo)入數(shù)據(jù)庫的數(shù)據(jù),在這個步驟中,我們必須創(chuàng)建uploads文件夾來存儲excel文件到這個文件中,然后讀取該文件。

代碼如下:

excelUpload.php

<?php

require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');

if(isset($_POST['Submit'])){

  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
  if(in_array($_FILES["file"]["type"],$mimes)){

    $uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);

    $Reader = new SpreadsheetReader($uploadFilePath);

    $totalSheet = count($Reader->sheets());

    echo "你有 ".$totalSheet." 張表".

    $html="<table border='1'>";
    $html.="<tr><th>標(biāo)題</th><th>描述</th></tr>";

    for($i=0;$i<$totalSheet;$i++){

      $Reader->ChangeSheet($i);

      foreach ($Reader as $Row)
      {
        $html.="<tr>";
        $title = isset($Row[0]) ? $Row[0] : '';
        $description = isset($Row[1]) ? $Row[1] : '';
        $html.="<td>".$title."</td>";
        $html.="<td>".$description."</td>";
        $html.="</tr>";

        $query = "insert into items(title,description) values('".$title."','".$description."')";
        $mysqli->query($query);
       }
    }
    $html.="</table>";
    echo $html;
    echo "<br />添加到數(shù)據(jù)庫的數(shù)據(jù)";
  }else { 
    die("<br/>sorry,不允許此文件類型上傳,只允許Excel文件。"); 
  }
}
?>

感謝你能夠認真閱讀完這篇文章,希望小編分享PHP將excel文件導(dǎo)入mysql數(shù)據(jù)庫的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI