PHP Import Excel data to mysql_php tutorial

Source: Internet
Author: User
Example: 1. Import only one sheets Excel file Require_once ("db.php"); Reference database instantiation class require_once ("reader.php"); Apply a class that imports Excel $data = new Spreadsheet_excel_reader (); Instantiate class $data->setoutputencoding (' utf-8 ');//Set Encoding $data->read ($_files["Excel" ["Tmp_name"]);//Read Excel temp file if ( $data->sheets[0][' numrows ']>0) {//Determine if the number of lines in Excel is greater than 0 lines $data->sheets[0][' NumRows ') is the total number of rows for Excel ($i = 2; $i <= $data->sheets[0][' numrows ']; $i + +) {//EXECL data is inserted into the database $i means reading from the $i line of Excel $sql = "INSERT INTO ' user ' (' user_name ', ' phone ', ' user_email ', ' password ') Values (' {$data->sheets[0][' cells ' [$i][1]} ',//$i is the line number in Excel ' {$data->sheets[0][' cells '] [$i][2]} ', ' {$data- >sheets[0][' cells '] [$i][3]} ', ' 123456 '); $db->query ($sql); }} 2 Import an Excel file that has multiple sheets in fact and import a sheets is the same if there are two sheets (and so on) require_once ("db.php"); Reference database instantiation class require_once ("reader.php"); Apply a class that imports Excel $data = new Spreadsheet_excel_reader (); Instantiate class $data->setoutputencoding (' utf-8 ');//Set Encoding $data->read ($_files["Excel" ["Tmp_name"]);//Read excEl temp file if ($data->sheets[0][' numrows ']>0) {//Determine if the number of lines in Excel is greater than 0 lines $data->sheets[0][' NumRows '] is the total number of Excel rows here $data->sheets[0] Represents the first sheets in Excel for ($i = 2; $i <= $data->sheets[0][' numrows '); $i + +) {//Inserts EXECL data into the database $i represents the E Xcel Line $i starts reading $sql = "INSERT INTO ' user ' (' user_name ', ' phone ', ' user_email ', ' Password ') values (' {$data->sheets[0][') Cells ' [$i][1]} ',//$i is the line number in Excel ' {$data->sheets[0][' cells '] [$i][2]} ', ' {$data->sheets[0][' cells ' [$i][3]} ' , ' 123456 ') "; $db->query ($sql); if ($data->sheets[1][' numrows ']>0) {//To determine if the number of lines in Excel is greater than 0 lines $data->sheets[0][' NumRows '] is the total number of Excel rows here Data->sheets[1] Represents the second sheets for ($i = 2; $i <= $data->sheets[1][' numrows '); $i + +) {//Inserts EXECL data into the database $i represents the $ from Excel I line starts reading $sql = "INSERT INTO ' user ' (' user_name ', ' phone ', ' user_email ', ' Password ') values (' {$data->sheets[1][' cells ') [$i] [1]} ',//$i is the line number in Excel ' {$data->sheets[1][' cells ' [$i][2]} ', ' {$data->sheets[1][' cells '] [$i][3]} ', ' 123456 ') " ; $db->query ($sql); } }

http://www.bkjia.com/PHPjc/477132.html www.bkjia.com true http://www.bkjia.com/PHPjc/477132.html techarticle Example: 1. Import only one sheets Excel file require_once (db.php);//reference DB Instantiation Class require_once (reader.php);//Apply import to Excel class $data = new Sp Readsheet ...

  • 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.