search
Homephp教程php手册thinkphp3.2 and phpexcel import

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                   $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                                                                                                                                                                                     }<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? !

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.