php 读取 excel 文件并上传数据库

版权声明:lie_sun版权所有 https://blog.csdn.net/weixin_43260760/article/details/83021626

1、首先接收前端发来的excel

	//接收前台文件
    public function addExcel()
    {
        //接收前台文件
        $ex = $_FILES['file'];
        //重设置文件名
        $filename = time() . substr($ex['name'], stripos($ex['name'], '.'));
        $path = 'excel/' . $filename;//设置移动路径
        move_uploaded_file($ex['tmp_name'], $path);
        //表用函数方法 返回数组
        $exfn = $this->_readExcel($path); // 读取内容
        $this->upload_file($exfn, $path); // 上传数据 
    }

2、接收到文件之后读取excel文件内容

	//创建一个读取excel数据,可用于入库
    public function _readExcel($path)
    {
        //引用PHPexcel 类
        include_once('util/PHPExcel.php');
        include_once('util/PHPExcel/IOFactory.php');//静态类
        $type = 'Excel2007';//设置为Excel5代表支持2003或以下版本,Excel2007代表2007版
        $xlsReader = PHPExcel_IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $xlsReader->setLoadSheetsOnly(true);
        $Sheets = $xlsReader->load($path);
        //开始读取上传到服务器中的Excel文件,返回一个二维数组
        $dataArray = $Sheets->getSheet(0)->toArray();
        return $dataArray;
    }

3、将读取的数据上传到数据库

	//将数据导入数据库
    private function upload_file($data, $path)
    {
        global $db;
        $arr = array();
        array_push($arr, $data[0]);
        //删除第一项
        unset($data[0]);
        $sql = 'insert into media_platform (user,phone,passwd,head,nickname,platform) values (?,?,?,?,?,?)';
        $stmt = $db->prepare($sql);
        foreach ($data as $v) {
            $result = $stmt->execute(array($v[0] ? $v[0] : '', $v[1] ? $v[1] : '', $v[2] ? $v[2] : '', $v[3] ? $v[3] : '', $v[4] ? $v[4] : '', $v[6] ? $v[6] : ''));
		    // $stmts->execute(array($v[6] ? $v[6] : ''));
            if (!$result) {
                array_push($arr, $v);
            }
        }
        echo json_encode($arr);
        unlink($path); // 上传完文件之后删除文件,避免造成垃圾文件的堆积
    }

笔者使用的是 mysql之PDO

注意:文件夹格式

在这里插入图片描述

PhpExcel下载地址https://github.com/PHPOffice/PHPExcel/tree/1.8/Classes

猜你喜欢

转载自blog.csdn.net/weixin_43260760/article/details/83021626
今日推荐