This article mainly introduces the Thinkphp5+phpexcel implementation of bulk upload form data function, the need for friends can refer to the following
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 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 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 bulk upload form data function, I hope to help you, if you have any questions please ask in our community questions oh.
Related recommendations:
How to implement no flush import data with Thinkphp,uploadify,upload,phpexcel
Phpexcel How to import an Excel code instance that handles big data
Bulk uploading of data using Phpexcel