How to import Excel data into database using PHP

不言
Release: 2023-03-31 14:08:01
Original
7615 people have browsed it

This article mainly introduces how to use PHP to import Excel data into the database. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

    /**
     * 导入excel
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function impExcel($filename,$data){
        if (!empty($_FILES)) {
            $config = array(
                'exts'=>array('xlsx','xls'),
                'rootPath'=>"./",
                'savePath'=> "Public/Uploads/$filename/",
                'subName'    =>    array('date','Ymd'),
            );
            $upload = new \Think\Upload($config);
            if (!$info=$upload->upload()) {
                $this->error($upload->getError());
            }

            vendor("PHPExcel.PHPExcel");
            $file_name = $info['excel']['savepath'].$info['excel']['savename'];

            //$objReader = \PHPExcel_IOFactory::createReader('Excel5');

            $Excel_name = getcwd() . '/' .$file_name;

            //$aaa = is_readable($Excel_name);

           // $objPHPExcel = $objReader->load($Excel_name,$encode='utf-8');
            $extension = strtolower( pathinfo($Excel_name, PATHINFO_EXTENSION) );

            if ($extension =='xlsx') {
                $objReader = new \PHPExcel_Reader_Excel2007();
                $objPHPExcel = $objReader ->load($Excel_name);
            } else if ($extension =='xls') {
                $objReader = new \PHPExcel_Reader_Excel5();
                $objPHPExcel = $objReader ->load($Excel_name);
            } else if ($extension=='csv') {
                $PHPReader = new \PHPExcel_Reader_CSV();

                //默认输入字符集
                $PHPReader->setInputEncoding('GBK');

                //默认的分隔符
                $PHPReader->setDelimiter(',');

                //载入文件
                $objPHPExcel = $PHPReader->load($Excel_name);
            }

            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $highestColumn = $sheet->getHighestColumn(); // 取得总列数

            for($i=2;$i<=$highestRow;$i++)
            {

                $data[&#39;name&#39;] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
                $data[&#39;tel&#39;] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                $data[&#39;zjh&#39;]    = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                $data[&#39;sheng&#39;] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                $data[&#39;shi&#39;] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                $data[&#39;address&#39;]= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                $data[&#39;money&#39;]= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                $data[&#39;hk_time&#39;]= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                $data[&#39;nanyi&#39;]= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                $data[&#39;uid&#39;] = $_SESSION[&#39;user&#39;][&#39;id&#39;];
                $data[&#39;time&#39;] = date("Y-m-d H:i:s");
                M(&#39;release&#39;)->add($data);
            }

            $this->success(&#39;导入成功!&#39;);exit();
        }else
        {
            $this->error("请选择上传的文件");
        }


    }
Copy after login
调用:
Copy after login
 if(IS_POST)
        {
            $data[&#39;reason&#39;] = I("post.reason","","trim");
            $data[&#39;type&#39;] = I("post.type","","trim");
           // $data[&#39;qixian&#39;] = I("post.qixian","","trim");
           // $data[&#39;comm&#39;] = I("post.comm","","trim");
            if(!empty($_FILES)){
                $this->impExcel(&#39;batch_release&#39;,$data);
            }
        }
Copy after login

The above is the entire content of this article , I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Code to export data and pictures using PHPEXCEL

The above is the detailed content of How to import Excel data into database using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template