Thinkphp Implementing methods for importing Excel into a database

Source: Internet
Author: User

Hint: I encountered an error while testing Exception ' phpexcel_reader_exception ' with message ' the filename '

The reason is because the file suffix of Excel may be different, my file suffix is xlsx, and then give the file that saves him as XLS, it can be

?
1234567891011    <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>

The following is the PHP

?
1234567891011121314151617181920212223242526272829303132333435363738 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("请选择上传的文件");   }}

More about thinkphp

Thinkphp Implementing methods for importing Excel into a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.