Import excel data to mysql using php

Source: Internet
Author: User
Instance: 1. import the excel file require_once (& quot; db. php & quot;); // reference the database instantiation class require_once (& quot; reader. php & quot;); // The excel class imported by the application $ data = newSpreadsheet_Excel _ instance: 1. import the excel file require_once ("db. php "); // reference the database instantiation class require_once (" reader. php "); // $ excel $ data = new Spreadsheet_Excel_Reader (); // instantiate the class $ data-> setOutputEncoding ('utf-8 '); // Set the encoding $ data-> read ($ _ FILES ["excel"] ["tmp_name"]); // read the excel temporary file if ($ data-> sheets [0] ['numrows ']> 0) {// Determine whether the number of rows in the excel file is greater than 0. $ data-> sheet [0] ['numrows '] indicates the total number of rows in the excel file for ($ I = 2; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {// insert execl data into the database $ I indicates reading from row $ I of excel $ SQL = "insert into 'user' ('User _ name', 'phone ', 'user _ email ', 'password') values (' {$ data-> sheets [0] ['cells '] [$ I] [1]}', // $ I is the row number '{$ data-> sheets [0] ['cells'] [$ I] [2]} 'in excel. '{$ data-> sheets [0] ['cells'] [$ I] [3]} ', '123 ')"; $ db-> query ($ SQL) ;}2 importing an excel file with multiple sheets is actually the same as importing a sheet. if there are two sheets (and so on) require_once ("db. php "); // reference the database instantiation class require_once (" reader. php "); // $ excel $ data = new Spreadsheet_Excel_Reader (); // instantiate the class $ data-> setOutputEncoding ('utf-8 '); // Set the encoding $ data-> read ($ _ FILES ["excel"] ["tmp_name"]); // read the excel temporary file if ($ data-> sheets [0] ['numrows ']> 0) {// Determine whether the number of rows in excel is greater than 0. $ data-> sheet [0] ['numrows '] indicates the total number of rows in excel. here, $ data-> sheet [0] the first sheet for ($ I = 2; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {// insert execl data into the database $ I indicates reading from row $ I of excel $ SQL = "insert into 'user' ('User _ name', 'phone ', 'user _ email ', 'password') values (' {$ data-> sheets [0] ['cells '] [$ I] [1]}', // $ I is the row number '{$ data-> sheets [0] ['cells'] [$ I] [2]} 'in excel. '{$ data-> sheets [0] ['cells'] [$ I] [3]} ', '123 ')"; $ db-> query ($ SQL);} if ($ data-> sheets [1] ['numrows ']> 0) {// Determine whether the number of rows in excel is greater than 0. $ data-> sheet [0] ['numrows '] indicates the total number of rows in excel. here, $ data-> sheet [1] the second sheets for ($ I = 2; $ I <= $ data-> sheets [1] ['numrows ']; $ I ++) {// insert execl data into the database $ I indicates reading from row $ I of excel $ SQL = "insert into 'user' ('User _ name', 'phone ', 'user _ email ', 'password') values (' {$ data-> sheets [1] ['cells '] [$ I] [1]}', // $ I is the row number '{$ data-> sheets [1] ['cells'] [$ I] [2]} 'in excel. '{$ data-> sheets [1] ['cells'] [$ I] [3]} ', '123 ')"; $ db-> query ($ SQL );}}

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.