This article mainly introduces the knowledge of PHP uploading Excel tables and acquiring data. Has a good reference value. Let's take a look at the little series.
One: First, the HTML section
Two: is to receive and process the uploaded files. PHP section
File storage Path $save_path = "/upload/"; File Folder $save_files = $this->geturl (); This is a new folder by month and day, for reference only. First check whether the current folder exists, if it does not exist, create a folder function Geturl () {$year = date (' Y '), $month = Date (' m '); $day = Date (' d '); $str = $year. $month. $ Day;if (Strtoupper (substr (php_os,0,3)) = = ' WIN ') {$path = GETCWD (). "/upload/". $str;} else{$path = "/mnt/erp/". $str;} if (!file_exists ($path))//Determine if the folder exists {mkdir ($path);} return $path. " /"; return $STR." /";} This is where you upload the file to the location you want to save, if (! @move_uploaded_file ($_files[$upload _name]["Tmp_name"], $save _path. $file _path)) {$error = " error| upload file error. "; Exit (0);}
Let's start getting the Excel data you've uploaded.
Gets the data for the upload table $file_name = $save _path. $file _path; Gets the address name of the uploaded file require_once APPPATH. ' views/idc/config/phpexcel.php '; Require_once APPPATH. ' views/idc/config/phpexcel/iofactory.php '; require_once APPPATH. ' views/idc/config/phpexcel/cell.php '; $objReader = Phpexcel_iofactory::createreader (' excel2007 '); Build Reader Object $objphpexcel = $objReader->load ($file _name); $sheet = $objPHPExcel->getsheet (); $highestRow = $ Sheet->gethighestdatarow (); Total number of rows $highestcolumn_num = phpexcel_cell::columnindexfromstring ($sheet->gethighestdatacolumn ()); Number of columns//$columns = Phpexcel_cell::getcolumn ($highestColumn _num); $columns = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '); $arr _ result = Array (), $dealer _element = Array (), for ($j = 2; $j <= $highestRow; $j + +) {for ($k = 0; $k < count ($columns); $k + +) {//Read cell $value = $objPHPExcel->getactivesheet ()->getcell ($columns [$k]. $j)->getvalue ();// This is to get the value of each cell $value = Trim ($value), if (Empty ($value)) {$value = NULL;} $dealer _element[$k] = $value;//Here you can do some validation of the data according to the requirements} $arr _result[$j] = $dealer _element;} Echo Json_encode ($arr _result);
The above is the whole content of this article, I hope that everyone's study has helped.