search
Homephp教程php手册php导入excel数据到mysql

php导入excel数据到mysql

Jun 13, 2016 am 10:58 AM
excelmysqlphpExampleimportdatadocumentof

实例:

 

1.导入只有一个sheets的excel文件

 

     require_once ("db.php");  //引用数据库实例化类

 

 

     require_once ("reader.php");  // 应用导入excel的类

 

 

 

     $data = new Spreadsheet_Excel_Reader();  //实例化类

 

 

    $data->setOutputEncoding('utf-8');//设置编码

    

    $data->read($_FILES["excel"]["tmp_name"]);//读取excel临时文件

 

 

    if ($data->sheets[0]['numRows']>0){   //判断excel里面的行数是不是大于0行  $data->sheets[0]['numRows']是excel的总行数

      

      for ($i = 2; $i sheets[0]['numRows']; $i++) {  //将execl数据插入数据库  $i表示从excel的第$i行开始读取  

              $sql="insert into `user` (`user_name`,`phone`,`user_email`,`password`) values(                 

                    '{$data->sheets[0]['cells'][$i][1]}',  //$i是excel中的行号

                    '{$data->sheets[0]['cells'][$i][2]}',

                   '{$data->sheets[0]['cells'][$i][3]}',

                   '123456'

             )";

        $db->query($sql);

      }

            

     }

 

 

 

2 导入存在多个sheets的excel文件

 

 其实和导入一个sheets是一样的 如果有两个sheets(以此类推)

 

require_once ("db.php");  //引用数据库实例化类

 

 

     require_once ("reader.php");  // 应用导入excel的类

 

 

 

     $data = new Spreadsheet_Excel_Reader();  //实例化类

 

 

    $data->setOutputEncoding('utf-8');//设置编码

    

    $data->read($_FILES["excel"]["tmp_name"]);//读取excel临时文件

 

 

if ($data->sheets[0]['numRows']>0){ //判断excel里面的行数是不是大于0行 $data->sheets[0]['numRows']是excel的总行数  这里的$data->sheets[0]表示excel中的第一sheets

      

      for ($i = 2; $i sheets[0]['numRows']; $i++) {  //将execl数据插入数据库  $i表示从excel的第$i行开始读取  

              $sql="insert into `user` (`user_name`,`phone`,`user_email`,`password`) values(                 

                    '{$data->sheets[0]['cells'][$i][1]}',  //$i是excel中的行号

                    '{$data->sheets[0]['cells'][$i][2]}',

                   '{$data->sheets[0]['cells'][$i][3]}',

                   '123456'

             )";

        $db->query($sql);

      }

 

 

 

 if ($data->sheets[1]['numRows']>0){   //判断excel里面的行数是不是大于0行  $data->sheets[0]['numRows']是excel的总行数 这里的$data->sheets[1]表示第二个sheets

      for ($i = 2; $i sheets[1]['numRows']; $i++) {  //将execl数据插入数据库  $i表示从excel的第$i行开始读取  

              $sql="insert into `user` (`user_name`,`phone`,`user_email`,`password`) values(                 

                    '{$data->sheets[1]['cells'][$i][1]}',  //$i是excel中的行号

                    '{$data->sheets[1]['cells'][$i][2]}',

                   '{$data->sheets[1]['cells'][$i][3]}',

                   '123456'

             )";

        $db->query($sql);

      }

 

            

     }

 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function