Home  >  Article  >  Backend Development  >  thinkPHP implements the method of importing excel into the database, thinkphpexcel_PHP tutorial

thinkPHP implements the method of importing excel into the database, thinkphpexcel_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:53:52752browse

thinkPHP implements the method of importing excel into the database, thinkphpexcel

This article describes the example of thinkPHP implementing the method of 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 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 an xls file and that’s it.

<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 php

function abcdefgwulisuibianuplod(){
  $this->display();//显示页面
}
function abcdefgwulisuibian(){
  if (!empty($_FILES)) {
   import("@.ORG.UploadFile");
   $config=array(
    '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 on this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".

I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122889.htmlTechArticlethinkPHP implements the method of importing excel into the database, thinkphpexcel This article describes how thinkPHP implements the method of importing excel into the database. method. Share it with everyone for your reference, specifically...
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