TP3.2 in Phpexcel import Excel method sharing

Source: Internet
Author: User
Tags import database
This article mainly share with you TP3.2 Phpexcel Import Excel method, hope can help everybody.







Specific code:

Vendor (' Phpexcel.phpexcel ');p ublic function Excel_runimport () {$PHPExcel =new \phpexcel ();        if (! empty ($_files [' file '] [' name ']) {$file _types = explode (".", $_files [' file '] [' name ']);        $exts = $file _types [count ($file _types)-1]; /* discriminant is not an. xls file, discriminant is not an Excel file */if (Strtolower ($exts)! = "Xlsx" | | strtolower ($exts)! = "xls") {$        This->error (' Not Excel file, re-upload ');        }//Generate a unique ID $filename = MD5 (Uniqid (Microtime (True), true)); $config =array (' maxSize ' =>70000000, ' exts ' =>array (' xlsx ', ' xls '), ' rootpath ' and ' = './uplo ads/excel/',//save filename ' savename ' = $filename,//Open subdirectory ' subn        Ame ' =>array (' Date ', ' YMD '),);        $upload =new \think\upload ($config);        $info = $upload->upload ();            if ($info) {if ($exts = = ' xls ') {import ("Vendor.PHPExcel.Reader.Excel5"); $PHPReader =new \phpexcel_reader_excel5 ();            }else if ($exts = = ' xlsx ') {import ("Vendor.PHPExcel.Reader.Excel2007");        $PHPReader =new \phpexcel_reader_excel2007 ();        } $rootPath = './uploads/excel/'; $savePath = $info [' file '] [' savepath '];         $saveName = $info [' file '] [' savename '];        Load file Creation Object $filePath = $rootPath. $savePath. $saveName; $objReader = $PHPReader->load ($filePath);        Gets the first sheet in the table, if you want to get the second one, change 0 to 1, and so on $currentSheet = $objReader->getsheet (0);        Gets the total number of columns $allColumn = $currentSheet->gethighestcolumn ();        Gets the total number of rows $allRow = $currentSheet->gethighestrow (); Loops through the data in the table, $currentRow represents the current row, starting from which row to read the data, the index value starts at 0 $data = array ();//Create an empty array to receive tabular data//start with the first line of the loop for ($rowIndex =2; $rowIndex <= $allRow; $rowIndex + +) {//iterate to read the contents of each cell.                        Note that the row starts at 1, the column starts from A//Loop column for ($colIndex = ' A '; $colIndex <= $allColumn; $colIndex + +) {                        $addr = $colIndex. $rowIndex; $cell = $currentSheet->getcell ($ADDR)->getvalue (); if ($cell instanceof Phpexcel_richtext) {//Rich text conversion string $cell = $cell-&gt                        ; __tostring ();            } $data [$rowIndex] [$colIndex] = $cell;            }} if (Is_file ($filename)) unlink ($filename);            }else{echo $upload->geterror ();    }//$this->success (' Import Database succeeded ', U (' Excel_import '), 1); }}


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.