Home > Backend Development > PHP Tutorial > How to import excel files into mysql database in php_PHP tutorial

How to import excel files into mysql database in php_PHP tutorial

WBOY
Release: 2016-07-13 10:01:49
Original
895 people have browsed it

How to import excel files into mysql database in php

This article mainly introduces the method of importing excel files into mysql database in php, and analyzes the skills and techniques of phpexcel class to operate excel files. The method of importing the database has certain reference value. Friends in need can refer to it

The example in this article describes how to import excel files into mysql database in php. Share it with everyone for your reference. The specific analysis is as follows:

When php imports excel files into mysql database, we need to use a phpexcel class file. With this class file, we can quickly and easily import excel into mysql database. Here is an example to explain the details. Usage.

We need to prepare a database before importing. The sql statement code is as follows:

The code is as follows:

/*
Navicat MySQL Data Transfer

Source Server: 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 class file. This can be downloaded by Baidu search. The code is as follows:

The code is as follows:

if($_FILES['execl']['name']){
$db = mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names gbk');
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP936');
$data->read($_FILES['execl']['name']);
error_reporting(E_ALL ^ ​​E_NOTICE);
$sql = "";
for($i=1;$i<=$data->sheets[0]['numRows'];$i )
{
if($data->sheets[0]['cells'][$i][1]!=""){
$sql = "INSERT INTO `execl`(`name`)values('".$data->sheets[0]['cells'][$i][2]."');";
if(mysql_query($sql)){
echo 'success';
}else{
die('failed');
}
}
}
}
?>







I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971926.htmlTechArticleHow to import excel files to mysql database in php. This article mainly introduces the method of importing excel files to mysql database in php. , analyzed the skills of phpexcel class to operate excel files and import data...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template