PHP implementation upload Excel table and get data

Source: Internet
Author: User
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.


Related Article

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.