/*uploading data in bulk*/if(isset($_post[' Submit ']) &&$_post[' Submit ']== ' upload file '){ //Importing class files require_once(".. /classes/phpexcel.php "); require_once(".. /classes/phpexcel/iofactory.php "); //determine if the file is not selected if($_files[' File '] [' Size ']==0) { Echo"<script>alert (' you have not selected any files, please re-select! '); History.go ( -1);</script> "; } //limit the size of the upload table to a maximum of 5M $file _size=$_files[' File '] [' Size ']; $maxSize=5242880; if($file _size>=$maxSize) { Echo"<script>alert (' upload failed, the size of the upload form cannot exceed 5M '); History.go ( -1);</script>"; } //detecting the type of upload file $type=$_files[' File '] [' Type ']; $type 1= ' Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '; $type 2=Application/vnd.ms-excel‘; if(($type!==$type 1) && ($type!==$type 2)) { Echo"<script>alert (' the file you uploaded is not an Excel table! '); History.go ( -1);</script> "; } //for different types of Excel files, pass in different phpexcel classes if($type==$type 1) { require_once(".. /classes/phpexcel/reader/excel2007.php "); $objReader= \phpexcel_iofactory::createreader (' Excel2007 '); } ElseIf($type==$type 2) { require_once(".. /classes/phpexcel/reader/excel5.php "); $objReader= \phpexcel_iofactory::createreader (' Excel5 '); } Else { Echo"<script>alert (' the file you uploaded is not an Excel table! ') </script> "; } //determine if the server was successfully passed in and take out the tables placed in the cache if(Is_uploaded_file($_files[' File '] [' Tmp_name '])) { $objPHPExcel=NewPhpexcel (); $filename=$_files[' File '] [' Tmp_name ']; $objPHPExcel= Phpexcel_iofactory::load ($filename); //get the form, number of rows, and number of columns $sheet=$objPHPExcel->getsheet (0); $highestRow=$sheet-Gethighestrow (); $highestColumn=$sheet-Gethighestcolumn (); $usefulColumn= Phpexcel_cell::columnindexfromstring ($highestColumn); $cell= ' '; $i=0; //iterate through the Insert database for($k= 2;$k<=$highestRow;$k++){ for($j= 0;$j<$usefulColumn;$j++){ $colIndex= Phpexcel_cell::stringfromcolumnindex ($j); $address=$colIndex.$k; $cell. = "'".addslashes($sheet->getcell ($address)->getvalue ()). "'". ', '; } $strValue=substr($cell, 0,-1); $sql= "INSERT into page VALUES (NULL,$strValue)"; $result=Mysqli_query($link,$sql); $i++; $cell= ' '; } if($result) { Echo"<script>alert (' successful import ').$i." Data '); Window.location.href= '. /view/addpaper.php ';</script> '; } Else { Echo"<script>alert (' Add failed! '); window.location.href= '. /view/addpaper.php ';</script> '; Exit(); } } Else { Echo"<script>alert (' upload failed! '); History.go ( -1);</script> "; }}
Bulk upload of Excel table data to MySQL database using Phpexcel