How to use Phpexcel to realize the function of bulk upload table

Source: Internet
Author: User
In many cases we will need to upload a large number of tables to the database, then today to teach you how to implement bulk upload form PHP.

First of all to download Phpexcel into the vendor folder, my path is: Project/vendor/phpexcel/, the download of the Phpexcel file here

Front-end Code

<! DOCTYPE html>


Background code

/** * Import Tabular Data * First upload the file to the server, then read the data to the database */Public Function Importexcel () {Header ("Content-type:text/html;charset=utf    -8 ");    Upload Excel file $file = Request ()->file (' myfile '); Move to/public/uploads/excel/$info = $file->move (root_path. ' Public '). DS. ' Uploads '.    DS. ' Excel ');      Upload file successfully if ($info) {//Introduce Phpexcel class vendor (' PHPExcel.PHPExcel.Reader.Excel5 ');      Gets the file name after uploading $fileName = $info->getsavename ();      File path $filePath = ' public/uploads/excel/'. $fileName;      Instantiate the Phpexcel class $PHPReader = new \phpexcel_reader_excel5 ();      Read Excel file $objPHPExcel = $PHPReader->load ($filePath);      Reads the first worksheet in an Excel file $sheet = $objPHPExcel->getsheet (0);  $allRow = $sheet->gethighestrow ();  Total number of rows obtained//$allColumn = $sheet->gethighestcolumn (); Get total number of columns//starting from the second line, the first row is the column name for ($j =2; $j <= $allRow; $j + +) {$data [' name '] = $objPHPExcel->getactive        Sheet ()->getcell ("A". $j)->getvalue (); $data [' Tel '] = $objPHPExcel->getactivesheet ()->getcell ("B". $j)->getvalue ();        $data [' addr '] = $objPHPExcel->getactivesheet ()->getcell ("C". $j)->getvalue (); $last _id = db::table (' users ')->insertgetid ($data);//Save the data and return the primary key ID if ($last _id) {echo "No. $j." Row import succeeds, users table: ". $last _id." Reviews        <br/> "; }else{echo "First". $j. " Row Import failed!        <br/> "; }}}else{echo "Failed to upload the file!    "; }  }

Output results

Attention:

The introduction of third-party class libraries using vendor () is in the form of namespaces. The underlying code will take the ". "Automatically replaced with"/", so Use"/". instead

The above code can be copied to use directly, but the database related information to change to your own!


Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

PHP deployment Environment using GIT

Some use cases of git

JavaScript data types and git use code explained

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.