How to import Excel data into database using PHP
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['name'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $data['tel'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data['zjh'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data['sheng'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); $data['shi'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(); $data['address']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue(); $data['money']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue(); $data['hk_time']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue(); $data['nanyi']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue(); $data['uid'] = $_SESSION['user']['id']; $data['time'] = date("Y-m-d H:i:s"); M('release')->add($data); } $this->success('导入成功!');exit(); }else { $this->error("请选择上传的文件"); } }
调用:
if(IS_POST) { $data['reason'] = I("post.reason","","trim"); $data['type'] = I("post.type","","trim"); // $data['qixian'] = I("post.qixian","","trim"); // $data['comm'] = I("post.comm","","trim"); if(!empty($_FILES)){ $this->impExcel('batch_release',$data); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
