在这篇文章中,我将给大家介绍如何使用PHP将excel文件导入mysql数据库。有时候我们需要从管理面板添加数据,如产品,项目,用户,电子邮件等。如果我们的数据很少,那么手工添加就可以了,但是如果我们的excel文件或者csv文件的数据比较多,那么存储数据的时间就比较长,这时我们就需要直接导入xls文件或者csv文件到mysql数据库中。

PHP如何将excel文件导入mysql数据库?-LMLPHP

下面我们将使用Spreadsheet_Excel_Reader类将excel文件导入php数据库,步骤如下:

1.下载类库

2.创建db_config.php文件

3.创建index . php文件

4.创建excelUpload.php

5.创建上传文件夹

步骤1:下载类库

从GitHub下载PHP Excel Reader库,下载地址:https://github.com/nuovo/spreadsheet-reader

下载后将其解压缩到根目录并将其重命名为“library”。

步骤2:创建db_config.php文件

为数据库配置创建db_config.php文件,在这个文件中,你必须设置数据库主机、数据库用户名、数据库密码、数据库名称。该文件将用于将数据存储到数据库中。

代码如下:

db_config.php

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

步骤3:创建index.php文件

在根目录中创建index.php文件,在这个文件中,我使用bootstrap创建了一个简单的表单,实现点击按钮后导入选择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">
	<h1>Excel上传</h1>
	<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文件导入mysql数据库?-LMLPHP

步骤4:创建excelUpload.php文件

创建excelUpload.php文件来管理导入数据库的数据,在这个步骤中,我们必须创建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>标题</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 />添加到数据库的数据";
  }else { 
    die("<br/>sorry,不允许此文件类型上传,只允许Excel文件。"); 
  }
}
?>
登录后复制

相关视频教程推荐:《PHP教程》《mysql教程

本篇文章就是关于PHP将excel文件导入mysql数据库的方法介绍,希望对需要的朋友有所帮助!

以上就是PHP如何将excel文件导入mysql数据库?的详细内容,更多请关注Work网其它相关文章!

09-14 18:16