Php-excel-reader reads excel content and saves it to the database

Source: Internet
Author: User
In the previous article, we introduced how to use php-excel-reader to read excel files. to read data like excel, create a database table as follows: -- database: 'alumni' -- table structure 'Alumni

The previous article introduced how to use php-excel-reader to read excel files. as needed, convert data such as excel:

Create a database table as follows:

-- Database: 'alumni'

-- Table structure 'alumni'

Create table if not exists 'alumni '(

'Id' bigint (20) not null AUTO_INCREMENT,

'Gid' varchar (20) default null comment 'file number ',

'Student _ no' varchar (20) default null comment 'student id ',

'Name' varchar (32) default null,

Primary key ('id '),

KEY 'gid' ('gid '),

KEY 'name' ('name ')

) ENGINE = MyISAM default charset = utf8;

The database results after import are as follows:

The php source code is as follows:


 Query ('set names utf8; ');} catch (PDOException $ e) {echo "connection failed ". $ e-> getMessage () ;}// pdo binding parameter operation $ stmt = $ dbh-> prepare ("insert into alumni (gid, student_no, name) values (: gid,: student_no,: name) "); $ stmt-> bindParam (": gid ", $ gid, PDO: PARAM_STR); $ stmt-> bindParam (": student_no ", $ student_no, PDO: PARAM_STR); $ stmt-> bindParam (": name", $ name, PDO: PARAM_STR ); // use php-excel-reader to read excel content $ data = new Spreadsheet_Excel _ Reader (); $ data-> setOutputEncoding ('utf-8'); $ data-> read ("stu.xls"); for ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {for ($ j = 1; $ j <= 3; $ j ++) {$ student_no = $ data-> sheets [0] ['cells '] [$ I] [1]; $ name = $ data-> sheets [0] ['cells '] [$ I] [2]; $ gid = $ data-> sheets [0] ['cells '] [$ I] [3];} // Insert the obtained excel content to the database $ stmt-> execute ();} echo "executed successfully"; echo "last inserted ID :". $ dbh-> lastInsertId ();?>

Considering that the excel volume is large, the PDO binding operation is used!

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.