Example of importing an excel file into a mysql database using php

Source: Internet
Author: User
To import an excel file from php to the mysql database, we need to use a phpexcel file. with this file, we can quickly and easily import an excel file to the mysql database, the following is an example for you... to import an excel file from php to the mysql database, we need to use a phpexcel file. with this file, we can quickly and easily import an excel file to the mysql database, the following is an example to help you.

Prepare a database before import. the SQL statement code is as follows:

/* Navicat MySQL Data Transfer  Source Server         : www.phprm.com localhost Source Server Version : 50133 Source Host           : localhost:3306 Source Database       : test  Target Server Type    : MYSQL Target Server Version : 50133 File Encoding         : 65001  Date: 2011-10-11 14:11:38 */  SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `execl` -- ---------------------------- DROP TABLE IF EXISTS `execl`; CREATE TABLE `execl` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `name` varchar(20) DEFAULT NULL,   PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;  -- ---------------------------- -- Records of execl -- ---------------------------- INSERT INTO `execl` VALUES ('14', 'jim'); INSERT INTO `execl` VALUES ('15', 'taurus');

Php processing program. here we need to use a phpexcel file, which can be searched and downloaded by Baidu. the code is as follows:

 SetOutputEncoding ('cp936'); $ data-> read ($ _ FILES ['execl'] ['name']); error_reporting (E_ALL ^ E_NOTICE ); $ SQL = ""; for ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {// open source code phprm.com if ($ data-> sheets [0] ['cells '] [$ I] [1]! = "") {$ SQL = "INSERT INTO 'execl' ('name') values ('". $ data-> sheets [0] ['cells '] [$ I] [2]. "');"; if (mysql_query ($ SQL) {echo 'successfully';} else {die ('failed ');}}}}      



Link to this article:

Add to favorites ^ please keep the tutorial address.

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.