The example in this article describes how thinkPHP implements importing excel into the database. Share it with everyone for your reference, the details are as follows:
PHPExcel plug-in can be downloaded from this website by clicking here.
The 3.1 version of the thinkphp framework is used here. Download the compressed package, create a new folder named PHPExcel in the vendor folder in the extend in the framework, and put the contents of classes into it
The following is the front-end page
Tips: I encountered an error exception 'PHPExcel_Reader_Exception' with message 'The filename
during testing. The reason is that the file suffix of excel may be different. My file suffix is xlsx, and then I save it as xls. The file is fine
<html> <head> </head> <body> <form action="{pigcms::U('Jdb/abcdefgwulisuibian')}" method="post" enctype="multipart/form-data"> <input type="file" name="import"/> <input type="hidden" name="table" value="tablename"/> <input type="submit" value="导入"/> </form> </body> </html>
The following is the PHP file
function abcdefgwulisuibianuplod(){ $this->display();//显示页面 } function abcdefgwulisuibian(){ if (!empty($_FILES)) { import("@.ORG.UploadFile"); $c 'allowExts'=>array('xlsx','xls'), 'savePath'=>'./Public/upload/', 'saveRule'=>'time', ); $upload = new UploadFile($config); if (!$upload->upload()) { $this->error($upload->getErrorMsg()); } else { $info = $upload->getUploadFileInfo(); } vendor("PHPExcel.PHPExcel"); $file_name=$info[0]['savepath'].$info[0]['savename']; $objReader = PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($file_name,$encode='utf-8'); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 for($i=2;$i<=$highestRow;$i++)//这个地方根据需要,一般第一行是名称,所以从第二行开始循环,也可以从第一行开始 { $data['lianjieid'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//数据库字段和excel列相对应 $data['yaoqingma'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data['dlmima']= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data['ljdizhi']= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); M('jdb')->add($data);//插入数据库 } $this->success('导入成功!'); }else { $this->error("请选择上传的文件"); } }
Readers who are interested in more thinkPHP related content can check out the special topics of this site: "ThinkPHP Getting Started Tutorial", "Summary of Common Methods of ThinkPHP", "Basic Tutorial for Getting Started with Smarty Templates" and "Summary of PHP Template Technology".
I hope this article will help you design PHP programs based on the ThinkPHP framework.
The above introduces the thinkPHP method of importing excel into the database, including the content of thinkphp. I hope it will be helpful to friends who are interested in PHP tutorials.