PHP uses Excelparser to import Excel into MySQL database

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array class content data data acquisition error file function

PHP Tutorial Import Excel into the MySQL Tutorial database tutorial, using Excelparser


class Excelparser


{


private $_data=array (0, "");


private $_excel_handle;


private $_excel=array ();


    /**


* Constructor


* @param <string> $filename upload file temporary file name


     */


Public Function __construct ($filename)


    {


        /**


* introduced Excelparser class


* Common method for


* requires path. ' Excelparser.php ';


         */


Import (' @. Util.PHPExcelParser.excelparser ', ', '. php ');


$this->_excel_handle=new excelfileparser ();


//Error getting


$this->checkerrors ($filename);


    }


    /**


* Error Checking


     */


Private Function Checkerrors ($filename)


    {


        /**


* Method A


         */


$error _code= $this->_excel_handle->parsefromfile ($filename);


/**


* Method Two


* $file _handle = fopen ($this->_filename, ' RB ');


* $content = fread ($file _handle,filesize ($this->_filename));


* fclose ($file _handle);


* $error _code = $this->_excel->parsefromstring ($content);


* unset ($content, $file _handle);


         */


switch ($error _code)


        {


Case 0:


No error not handling


break;


Case 1:


$this->_data=array (1, ' File read error (Linux note read-write permission) ');


break;


Case 2:


$this->_data=array (1, ' file too small ');


break;


Case 3:


$this->_data=array (1, ' www.jzread.com reads Excel table header failed ');


break;


Case 4:


$this->_data=array (1, ' file read error ');


break;


Case 5:


$this->_data=array (1, ' file may be empty ');


Break;


Case 6:


$this->_data=array (1, ' incomplete file ');


break;


Case 7:


$this->_data=array (1, ' Read data error ');


break;


Case 8:


$this->_data=array (1, ' version error ');


break;


        }


unset ($error _code);


    }


    /**


* Excel Information Access


     */


Private Function Getexcelinfo ()


    {


if (1== $this->_data[0]) return;


        /**


* Access to sheet quantity


* Gets the row and column corresponding to the sheet cell


*/


$this->_excel[' Sheet_number ']=count ($this->_excel_handle->worksheet[' name '));


for ($i =0 $i < $this->_excel[' sheet_number '; $i + +)


        {


            /**


* row in column


* Note: Count
starting from 0

             */


$row = $this->_excel_handle->worksheet[' data ' [$i] [' Max_row '];


$col = $this->_excel_handle->worksheet[' data '] [$i] [' Max_col '];


$this->_excel[' row_number ' [$i]= ($row ==null) 0:++ $row;


$this->_excel[' col_number ' [$i]= ($col ==null) 0:++ $col;


unset ($row, $col);


        }


    }


    /**


* Chinese processing function


* @return <string>


     */


Private Function uc2html ($STR)


    {


$ret = ';


For ($i =0 $i <strlen ($STR)/2; $i + +)


        {


$charcode = Ord ($str [$i *2]) +256*ord ($str [$i *2+1]);


$ret. = ' &# '. $charcode. ';


        }


return mb_convert_encoding ($ret, ' UTF-8 ', ' html-entities ');


    }


    /**


* Excel Data Acquisition


     */


Private Function Getexceldata ()


    {


if (1== $this->_data[0]) return;

//Modify Tag


$this->_data[0]=1;


//Get Data


for ($i =0 $i < $this->_excel[' sheet_number '; $i + +)


        {


            /**


* On-line cycling


             */


for ($j =0; $j < $this->_excel[' row_number '] [$i]; $j + +)


            {


                /**


*
the column loop

                 */


for ($k =0; $k < $this->_excel[' Col_number '] [$i]; $k + +)


                {


                    /**


* Array (4) {


* [' type '] => type [0 character type 1 integer 2 floating-point number 3rd]


* [Font] => font


* ["Data"] => data


                     *   ...


                     * }


                     */


$data = $this->_excel_handle->worksheet[' data ' [$i] [' cell '] [$j] [$k];


Switch ($data [' type '])


                    {


Case 0:


//character type


if ($this->_excel_handle->sst[' Unicode '] [$data [' Data ']])


{


//Chinese processing


$data [' data '] = $this->uc2html ($this->_excel_handle->sst[' data '] [$data [' Data ']]);


                            }


else


                            {


$data [' data '] = $this->_excel_handle->sst[' data '] [$data [' data ']];


                            }


break;


Case 1:


//Integer


//todo


break;


Case 2:


Floating-point


//todo


break;


Case 3:


//Date


TODO


break;


                    }


$this->_data[1][$i] [$j] [$k]= $data [' Data '];


unset ($data);


                }


}


        }


    }


    /**


* Main function


* @return <array> Array (identifier, content s)


     */


Public Function main ()


    {


//excel Information Acquisition


$this->getexcelinfo ();


//excel Data Acquisition


$this->getexceldata ();


return $this->_data;


    }


}

?>

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.