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

来源:藏色散人 发布时间:2019-03-13 10:34:44 阅读量:953

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

下面我们将使用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

1

2

3

4

5

6

7

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!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>

前台样式如下:

ea50f2c77fd20fdf3f0627b1c988761.png

步骤4:创建excelUpload.php文件

创建excelUpload.php文件来管理导入数据库的数据,在这个步骤中,我们必须创建uploads文件夹来存储excel文件到这个文件中,然后读取该文件。

代码如下:

excelUpload.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<?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 环境搭建
分享:
评论:
你还没有登录,请先