Home > php教程 > php手册 > body text

thinkphp3.2 and phpexcel import

WBOY
Release: 2016-07-06 13:29:21
Original
1188 people have browsed it

The most basic usage of importing thinkphp3.2 and phpexcel
Start with the most basic code. Once you understand this, the rest will be very simple$file_name= './Upload/excel/123456.xls';<br>         import("Org.Util.PHPExcel");<br>         import("Org.Util.PHPExcel.IOFactory");<br>         $objReader = PHPExcel_IOFactory::createReader('Excel5');<br>         $objPHPExcel = $objReader->load($file_name,$encode='utf-8');<br>         $sheet = $objPHPExcel->getSheet(0);<br>         $highestRow = $sheet->getHighestRow(); // 取得总行数<br>         $highestColumn = $sheet->getHighestColumn(); // 取得总列数<br>     <br>         $s = $objPHPExcel->getActiveSheet()->getCell("A2")->getValue();Table content:
thinkphp3.2 and phpexcel import
I’ll give you a little more difficulty and let’s talk about the idea first.
1. Upload the excel file and get its address
2. Write a function to process exl, that’s it
Example code demonstration: public function upload(){<br>         $files = $_FILES['exl'];<br>                                                                                                                // exl format, otherwise re-upload <br> If($files['type'] !='application/vnd.ms-excel'){<br>                  $this->error('Not an Excel file, please re-upload');                                                                  }<br>                                                               // Upload<br>         $upload = new ThinkUpload(); // Instantiate upload class<br>         $upload->maxSize                                                                                                                $upload->exts                                                                                                                                                                                   ​  $ Upload-& gt; rootpath = './upload/'; // Set the attachment to upload the root directory <br>          $upload->savePath =      'excel/'; // Set attachment upload (sub) directory <br>            //$upload->subName  =      array('date', 'Ym');<br>          $upload->subName                                                                                                       // Upload files <br> $info = $upload->upload();<br>  <br> $file_name = $upload->rootPath.$info['exl']['savepath'].$info['exl']['savename'];<br>          $exl = $this->import_exl($file_name);<br>                                                  ​​​​//Remove the first row in the exl table<br> ​​​​unset($exl[0]);<br>                                                                  // Clean up empty arrays<br> foreach($exl as $k=>$v){<br> If(empty($v)){<br> Unset ($ exl [$ k]); <br>                                                                                                    };<br> ​​​​//Reorder<br>        sort($exl);<br>                                                          $count = count($exl);<br> // After the test form is imported successfully, whether there is data generating <br> If($count<1){<br />                   $this->error('No valid data detected');                                                        }<br>                                                  ​​​​//Start combining data<br> foreach($exl as $k=>$v){<br>                                                               $goods[$k]['goods_sn'] = $v;<br>                                                                       // Query database               $where['goods_sn'] = array('like','%'.$v.'%');<br>                $res = M('goods')->where($where)->find();<br>                                                                                     $goods[$k]['goods_name'] = $res['goods_name'];<br>              $goods[$k]['goods_thumb'] = $res['goods_thumb'];<br>                 if($res){<br>                                                                                                                                                                                  ’ s being being matched successfully' s                    $goods[$k]['is_match']                                                                                       $f = 1;<br>                }else{<br>                                                                                                                                                                                                                          -- // Matching failed <br>                     $goods[$k]['is_match']                                                                                                                                                                                                                }<br>                                                                                    }<br>                  // Instantiation data<br>           $this->assign('goods',$goods);<br>             //print_r($f);<br>                                                  ​​​​//Statistical results<br>         $total['count'] = $count;<br>          $total['success'] = $f;<br>          $total['error'] = $w;<br>          $this->assign('total',$total);<br>                                                  ​​​​//Delete Excel file<br> ​​​​unlink($file_name);<br>           $this->display('info');<br>                                                                       }<br> /* Process uploaded exl data<br> * $file_name File path<br> */<br> Public function import_exl($file_name){<br> ​​​​ //$file_name= './Upload/excel/123456.xls';<br>           import("Org.Util.PHPExcel");                                                                                                                                                                                           .Import("Org.Util.PHPExcel.IOFactory");<br>          $objReader = PHPExcel_IOFactory::createReader('Excel5');<br>          $objPHPExcel = $objReader->load($file_name,$encode='utf-8');<br> $sheet = $objPHPExcel->getSheet(0);<br>          $highestRow = $sheet->getHighestRow(); // Get the total number of rows<br>          $highestColumn = $sheet->getHighestColumn(); // Get the total number of columns <br>                                                  for($i=1;$i<$highestRow 1;$i ){<br>                                                                                                                                                                                     }<br> Return $data; }<br>Perfect case of generating pictures with thinkphp3.2 and phpexcel<br> http://www.thinkphp.cn/code/2112.html <br> Yunqi Conference Beijing Station: It is rare for Alibaba technical experts to appear on the scene. More than 100 people came this time? !

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template