Bulk upload of Excel table data to MySQL database using Phpexcel

Source: Internet
Author: User

/*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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.