PHP imports Excel files into the database code

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

We are using this extension of excelfileparser to handle this, oh,

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
<html xmlns= "http://www.w3.org/1999/xhtml"
<head>
<meta http-equiv= "Content-type" content= "HTML; Charset=utf-8 "/>
<title>excel Data capture demo </title>
<meta name=" Keywords "content=" TODO "/>
<meta name= "Description" content= "TODO"/>
</head>
<body>
    < Div
      <div>excel data capture demo </div>
      <div>
        <form method= "POST" action=/index/parse "enctype=" Multipart/form-data "
            <input type=" File "Name=" Excel "value="/>
            <input Type= "Submission" name= "submit" value= "submitted"/>
        </form>
      </div>
     </DIV>
</body>
</html>


<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
File:
Summary:
*
* @author Week eight [Email=ixqbar@hotmail.com]ixqbar@hotmail.com[/email]
* @version
*/

class Indexaction extends Action


{


    /**


* Constructor


     */


Public Function __construct ()


    {


parent::__construct ();


    }


    /**


* Default index page


     */


Public Function index ()


    {


$this-&gt;display ();


}


    /**


* Submit Processing


     */


Public Function Parse ()


    {


       /**


* $_files Array Description


* Array (n) {


* [Form file box name] =&gt; Array (5) {


* ["name"] =&gt; Submit file name


* [' type '] =&gt; submit file type Excel as "Application/vnd.ms-excel"


* ["Tmp_name"] =&gt; temporary file name


* ["error"] =&gt; error (0 successful 1 files too large over upload_max_filesize2 file too large over max_file3 upload incomplete 4 no upload file)


* [size] =&gt; file size (in KB)


        *   }


        * }


        */


$return =array (0, "");


        /**


* Judge whether to submit


* Is_uploaded_file (file name) is used to determine whether the specified file is uploaded using the Post method, prevents illegal submission, and typically uses the Save upload file with Move_upload_file to the specified path


         */


if (!isset ($_files) | |!is_uploaded_file ($_files[' Excel '] [' tmp_name '])


        {


$return =array (1, ' submit illegal ');


        }


//Handling


if (0 = = $return [0])


        {


import (' @. Util.excelparser ');


$excel =new excelparser ($_files[' Excel '] [' tmp_name ']);


$return = $excel-&gt;main ();


        }


//Output processing


Print_r ($return);


    }


}


?&gt;




&lt;?php


/**


* CopyRight (c) 2009,


* All rights reserved.


* FileName: Excel data acquisition


* Abstract:


 *


* @author Week eight [email=ixqbar@hotmail.com]ixqbar@hotmail.com[/email]


* @version 0.1


 */


class Excelparser


{


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


private $_excel_handle;


private $_excel=array ();


    /**


* Constructor


* @param &lt;string&gt; $filename upload file temporary file name


     */


Public Function __construct ($filename)


    {


        /**


* introduced Excelparser class


* Common method for


* requires path. ' Excelparser.php ';


* Import is a thinkphp with imported class method


         */


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


$this-&gt;_excel_handle=new excelfileparser ();


//Error getting


$this-&gt;checkerrors ($filename);


    }


    /**


* Error Checking


     */


Private Function Checkerrors ($filename)


    {


        /**


* Method A


         */


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


/**


* Method Two


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


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


* fclose ($file _handle);


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


* unset ($content, $file _handle);


         */


switch ($error _code)


        {


Case 0:


No error not handling


break;


Case 1:


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


break;


Case 2:


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


break;


Case 3:


$this-&gt;_data=array (1, ' Read Excel header failed ');


break;


Case 4:


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


break;


Case 5:


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


Break;


Case 6:


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


break;


Case 7:


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


break;


Case 8:


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


break;


        }


unset ($error _code);


    }


    /**


* Excel Information Access


     */


Private Function Getexcelinfo ()


    {


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


        /**


* Access to sheet quantity


* Gets the row and column corresponding to the sheet cell


*/


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


for ($i =0 $i &lt; $this-&gt;_excel[' sheet_number '; $i + +)


        {


            /**


* row in column


* Note: Count
starting from 0

             */


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


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


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


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


unset ($row, $col);


        }


    }


    /**


* Chinese processing function


* @return &lt;string&gt;


     */


Private Function uc2html ($STR)


    {


$ret = ';


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


        {


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


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


        }


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


    }


    /**


* Excel Data Acquisition


     */


Private Function Getexceldata ()


    {


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

//Modify Tag


$this-&gt;_data[0]=1;


//Get Data


for ($i =0 $i &lt; $this-&gt;_excel[' sheet_number '; $i + +)


        {


            /**


* On-line cycling


             */


for ($j =0; $j &lt; $this-&gt;_excel[' row_number '] [$i]; $j + +)


            {


                /**


*
the column loop

                 */


for ($k =0; $k &lt; $this-&gt;_excel[' Col_number '] [$i]; $k + +)


                {


                    /**


* Array (4) {


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


* [Font] =&gt; font


* ["Data"] =&gt; data


                     *   ...


                     * }


                     */


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


Switch ($data [' type '])


                    {


Case 0:


//character type


if ($this-&gt;_excel_handle-&gt;sst[' Unicode '] [$data [' Data ']])


{


//Chinese processing


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


                            }


else


                            {


$data [' data '] = $this-&gt;_excel_handle-&gt;sst[' data '] [$data [' data ']];


                            }


break;


Case 1:


//Integer


//todo


break;


Case 2:


Floating-point


//todo


break;


Case 3:


//Date


TODO


break;


                    }


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


unset ($data);


                }


}


        }


    }


    /**


* Main function


* @return &lt;array&gt; Array (identifier, content s)


     */


Public Function main ()


    {


//excel Information Acquisition


$this-&gt;getexcelinfo ();


//excel Data Acquisition


$this-&gt;getexceldata ();


return $this-&gt;_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.