In reality, we often need such bulk import data in many places, in addition to Phpexcel and CSV and other methods can solve
Here is a detailed description of how to use
First in the official download installation package extracted to the local, and then copied to save to the vendor directory under the TP framework
The HTML page is as follows
Here is the back-end code:
Public Function column () {
$grade =d (' School ')->where (Array (' parent_id ' =>1))->select ();
$school = D (' Schooll ')->select ();
$this->assign (' School ', $school);
$this->assign (' Grade ', $grade);
if (is_post) {
$data _p = I (' post. ');
if ($data _p[' sno ']== ') {
$this->ajaxreturn (the ' nr ' = ' number already exists! ', ' SF ' = 0));
}elseif ($data _p[' sname ']== ') {
$this->ajaxreturn (' nr ' = ' name is not empty! ', ' SF ' = 0));
}elseif ($data _p[' grade ']== ') {
$this->ajaxreturn (' nr ' = ' not null ', ' SF ' = ' 0 ');
}elseif ($data _p[' major ']== ') {
$this->ajaxreturn (' nr ' = ' not null ', ' SF ' = ' 0 ');
}elseif ($data _p[' classs ']== ') {
$this->ajaxreturn (' nr ' = ' not null ', ' SF ' = ' 0 ');
}elseif ($data _p[' school ']== ') {
$this->ajaxreturn (' nr ' = ' not null ', ' SF ' = ' 0 ');
}else{
$student = M (' student ')->add (Array (' sno ' + = $data _p[' sno '], ' passwd ' =>md5 ($data _p[' passwd ']), ' sname ' =>$ data_p[' sname '], ' zt ' = = $data _p[' checkresult '], ' majorname ' = $data _p[' Major '], ' classname ' = $data _p[' Classs '], ' gradename ' = $data _p[' School '], ' schoolname ' = $data _p[' grade ']);
}
if ($student) {
$this->ajaxreturn (' nr ' = ' added successfully '. $data _p[' major '), ' sf ' = 0);
} else {
$this->ajaxreturn (' nr ' = + ' add failure! ', ' sf ' = + 0));
}
}
$this->display (' Index/column ');
}
Public function upload () {
if (!empty ($_files)) {
$config = Array (
' exts ' = = Array (' xlsx ', ' xls '),
' MaxSize ' = 3145728,
' RootPath ' = './excel/',
' SubName ' = Array (' Date ', ' YMD '),
);
$upload = new \think\upload ($config);
if (! $info = $upload->upload ()) {
$this->error ($upload->geterror ());
}else{
Vendor (' Phpexcel.phpexcel ');/////Introduction Class Library
Vendor (' PHPExcel.Reader.Excel5 ');
Import ("Vendor. Phpexcel ");
Import ("Vendor. PHPExcel.Reader.Excel5 ");
$objPHPExcel = new \phpexcel (); methods introduced by/////initialization
$PHPReader =new \phpexcel_reader_excel5 ();
$file _name= $upload->rootpath. $info [' Excel '] [' Savepath ']. $info [' Excel '] [' savename '];
}
$objReader = $PHPReader->load ($file _name);
$extension = Strtolower (PathInfo ($file _name, pathinfo_extension));
To determine the import table suffix format
if ($extension = = ' xlsx ') {
$objReader =\phpexcel_iofactory::createreader (' Excel2007 ');
$objPHPExcel = $objReader->load ($file _name, $encode = ' utf-8 ');
} elseif ($extension = = ' xls ') {
$objReader =\phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = $objReader->load ($file _name, $encode = ' utf-8 ');
}
$sheet = $objPHPExcel->getsheet (0);//Get the first sheet in a table
$highestRow = $sheet->gethighestrow ();//Get Total rows
$highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained
for ($currentRow =1; $currentRow <= $highestRow; $currentRow + +) {
for ($currentColumn = ' A '; $currentColumn <= $highestColumn; $currentColumn + +) {
$address = $currentColumn. $currentRow;
$arr [$currentRow] [$currentColumn]= $sheet->getcell ($address)->getvalue ();
}
for ($i = 2; $i <= $highestRow; $i + +) {
$data _p[' sno ') = $objPHPExcel->getactivesheet ()->getcell ("A". $i)->getvalue ();
$passwd = $objPHPExcel->getactivesheet ()->getcell ("B". $i)->getvalue ();
$data _p[' passwd ']=md5 ($PASSWD);
$data _p[' sname ') = $objPHPExcel->getactivesheet ()->getcell ("C". $i)->getvalue ();
$data _p[' zt '] = ' 0 ';
$data _p[' majorname ') = $objPHPExcel->getactivesheet ()->getcell ("D". $i)->getvalue ();
$data _p[' classname ') = $objPHPExcel->getactivesheet ()->getcell ("E". $i)->getvalue ();
$data _p[' gradename ') = $objPHPExcel->getactivesheet ()->getcell ("F". $i)->getvalue ();
$data _p[' schoolname ') = $objPHPExcel->getactivesheet ()->getcell ("G". $i)->getvalue ();
$ex =m (' Student ')->add ($data _p);
}
if ($ex) {
$this->success (' Import succeeded! ');
}else{
$this->error ("Please select the uploaded file");
}
}
}
Well, specifically, that's all. Where do not know can message [email protected] consulting.
thinkphp using Phpexcel to import files and write to the database