Http://www.jb51.net/article/129262.htm
1. First to download phpexcel into the vendor folder, my path is: Project/vendor/phpexcel/, the download of the Phpexcel file here
2. Front-end Code
<! DOCTYPE html>
3. Background code
/** * Import Table Data * Upload the file to the server before reading the data to the database*/ Public functionImportexcel () {Header("Content-type:text/html;charset=utf-8"); //Uploading Excel Files $file= Request ()file(' MyFile '); //move to/public/uploads/excel/ $info=$file->move (Root_path. ' Public '. DS. ' Uploads '. DS. ' Excel); //Upload file successfully if($info) { //introducing the Phpexcel classVendor (' PHPExcel.PHPExcel.Reader.Excel5 '); //get the file name after uploading $fileName=$info-Getsavename (); //file path $filePath= ' public/uploads/excel/'.$fileName; //instantiate the Phpexcel class $PHPReader=New\phpexcel_reader_excel5 (); //reading Excel Files $objPHPExcel=$PHPReader->load ($filePath); //reading 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//insert from second row, first row is column name for($j= 2;$j<=$allRow;$j++) { $data[' name '] =$objPHPExcel->getactivesheet ()->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"section".$j." Row import succeeds, users table: ".$last _id." Reviews <br/> "; }Else{ Echo"section".$j." Row Import failed! <br/> "; } } }Else{ Echo"Upload file failed!" "; } }
Output Result:
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!
Summarize
The above is a small part of the thinkphp5+phpexcel to introduce the implementation of the bulk upload form data function, I hope that we have some help, if you have any questions please give me a message, small series will
Original link: http://www.cnblogs.com/zxf100/archive/2017/11/28/7908659.htm
Thinkphp5+phpexcel enables bulk upload of tabular data functions