In the PHP programming language, the method of how to import the database in Excel table is a problem that many programmers have a headache, some technicians may have tried to search many different problems in Baidu, but the answer after their own test, found that still does not work, then to this, Brigitte Xuan Technology also tried a bit how to import the database in Excel table, now the source code to show you the technical programmer, we can learn from reference.
Public Function Saveexcel () {
Require_once ('./thinkphp/extend/vendor/phpexcel-1.8/classes/phpexcel.php ');
Require_once ('./thinkphp/extend/vendor/phpexcel-1.8/classes/phpexcel/iofactory.php ');
Require_once ('./thinkphp/extend/vendor/phpexcel-1.8/classes/phpexcel/reader/excel5.php ');
Get the size of the table, limit the size of the upload table to 5M
$file _size = $_files[' myfile ' [' Size '];
if ($file _size>51024x7681024) {
echo "<script>alert (' upload failed, upload form cannot exceed 5M size '); History.go ( -1);</script>";
Exit ();
// }
Limit upload table type $file _type = $_files[' myfile ' [' type ']; if ($file _type!= ' application/vnd.ms-excel ') {echo "<script>alert (' upload failed, upload only excel2003 or 97-2003 xls format! '); History.go ( -1) </script> "; Exit (); }//Determine if the form was successfully uploaded if (is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {//above three-step load phpexcel class $objReader = \phpexcel_iofactory::createreader (' Excel5 ');//use excel2007 for format//Receive Excel table in cache $filename = $ _files[' myfile ' [' tmp_name ']; $objPHPExcel = $objReader->load ($filename); $filename can be an uploaded form, or a specified table $sheet = $objPHPExcel->getsheet (0); $highestRow = $sheet->gethighestrow (); Total number of rows obtained//$highestColumn = $sheet->gethighestcolumn (); Get total columns//Loop through the Excel table, read one, insert a//j to start reading from the second line, because the first row is the title does not save//$a represents the column number $model =m (' user ') ; $rechargemodel =m (' recharge '); $joinmodel =m (' Joinplan '); for ($j =2; $j <= $highestRow; $j + +) { $name = $objPHPExcel->getactivesheet ()->getcell ("B". $j)->getvalue ();//Gets the value of the B (name) column $tel = $objPHPExcel ->getactivesheet ()->getcell ("C". $j)->getvalue ();//Gets the value of the C (cell phone number) column $arr [' name ']= $name; $arr [' Tel ']= $tel; $arr [' Addtime ']=time (); $arr [' Geti ']=0; $res = $model->add ($arr); } if ($res) {success (' Add Success ', U (' userlist ')); }else{error (' operation failed, please try again later '); } }
}
PHP Excel File
Code export (no need to introduce any interfaces)
Public Function Exportdayinner () {
$model = M ("Goodsinfo");
$innerdata = $mod->select ();
$list = $model->order (' gid desc ')->select ();
$smodel = M ("Shopinfo");
$gmodel = M ("Goodscategory");
foreach ($list as $key = = $value) {
$list [$key] [' Maoli ']=floatval ($value [' Price '])-floatval ($value [' jprice ']);
$list [$key] [' Maolilv ']=floatval ($list [$key] [' Maoli '])/floatval ($value [' Price ']) *100;
$list [$key] ["Catename"]= $gmodel->where ("id=%d", $value [' Goodscateid '])->getfield ("name");
}
$a =[' ordinary goods ', ' leisure area ', ' Special zone ', ' package area ', ' beverage area ', ' new product area ', ' limited purchase ', ' ordinary goods ';
$b = [' Recommended ', ' not recommended '];
$c = [' have ', ' none '];
echo $mod->getlastsql (); exit; Dump ($innerdata);d ie; $table = "; $table. = "<table align= ' center ' border= ' 1 ' cellpadding ' 0 ' cellspacing= ' 0 ' > <thead> <tr> <th class= ' name ' > serial number </th> <th class= ' name ' > Product name </t h> <th class= ' name ' > Product type </th> <th class= ' name ' > Commodity class </th> <th class= ' name ' > Product class two </th> <th class= ' name ' > recommended </th> <th class= ' name ' > Price </th> <th class= ' name ' > Price </th> <th class= ' name ' > Gross margin </th> <th class= ' name ' > Gross margin </th> <th class= ' name ' & gt; barcode </th> <th class= ' name ' > specification </th> <th class= ' name ' > production date </th .; <th class= ' name ' > shelf life </th> <th class= ' name ' > Recommended retail price </th> <th CLA ss= ' name ' > Availability </th> </tr> </thead> <tbody> '; foreach ($list as $v) {$table. = "<tr align= ' center ' > <TD CL ass= ' name ' >{$v [' gid ']}</td> <td class= ' name ' >{$v [' Gname ']}</td> < TD class= ' name ' >{$a [$v [' type ']]}</td> <td class= ' name ' >{$v [' Catename ']}</td> <TD class= ' name ' >{$v [' Classify ']}</td> <td class= ' name ' >{$b [$v [' Like ']]}< /td> <td class= ' name ' >{$v [' Jprice ']}</td> <td ' class= ' name ' >{$v [' pri Ce ']}/{$v [' Unity ']} </td> <td class= ' name ' >{$v [' Maoli ']}</td> <td C lass=' Name ' >{$v [' Maolilv ']}%</td> <td class= ' name ' >{$v [' Borcode ']}</td> <TD class= ' name ' >{$v [' norms ']}</td> <td class= ' name ' >{$v [' Ptime ']}</td> <TD class= ' name ' >{$v [' Shelflife ']}</td> <td class= ' name ' >{$v [' Retail_price ']}&L t;/td> <td class= ' name ' >{$c [$v [' Repertor ']]}</td> </tr>]; } $table. = "</tbody> </table>";
Output Excel table with header header control
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/vnd.ms-execl");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header (' content-disposition:attachment;filename= ' product list. xls "');
Header ("Content-transfer-encoding:binary");
Echo $table;
}
以上就是为各位讲解的如何在Excel表格中导入数据库的具体方法了,希望有不懂的技术人员可以在评论中提出,我们会为您解答。
This article by professional do app development, small program development, website construction of Zhengzhou Brigitte Xuan Software technology published, if need reprint please indicate the original author and source!
An explanation of how Excel tables are imported into the database in the PHP language