第一步:下载 thinkphp_3.2.3 和 PHPExcel_1.8.0 并解压
对应的网站分别为:
http://www.thinkphp.cn/down.html
https://github.com/PHPOffice/PHPExcel (已存百度云盘/PHP 扩展包/PHPExcel-1.8.zip)
第二步:
①将解压出来的ThinkPHP文件拷贝到网站根目录
②并创建index.php 代码如下
![](https://i-blog.csdnimg.cn/blog_migrate/12adf4d4ceec971680ca81d386673a1e.png)
第三步:
①在ThinkPHP\Library\Vendor目录下新建文件夹PHPExcel
②将PHPExcel_1.8.0_doc\Classes中的文件拷贝到ThinkPHP\Library\Vendor\
PHPExcel中
PHPExcel中
![](https://i-blog.csdnimg.cn/blog_migrate/a3a1b22a0eb6ecf285fce575f8ada3ae.png)
第四步:
①运行根目录的index.php文件,并手动创建Public文件夹
ThinkPHP欢迎界面:
![](https://i-blog.csdnimg.cn/blog_migrate/4007a700dd44a1d9b472b77f9df6b507.png)
根目录结构:
![](https://i-blog.csdnimg.cn/blog_migrate/cb7dfb689f6e1716e04f6d91f0c9b4a8.png)
②Application目录如下:
![](https://i-blog.csdnimg.cn/blog_migrate/1dcbaa53dcf2836deb785eea2f8df13d.png)
第五步:ThinkPHP连接我们的mysql数据库
文件位置 : 根目录/Application/Home/Conf/config.php
![](https://i-blog.csdnimg.cn/blog_migrate/043a30d135fe07a860d3e628f9058687.png)
第六步:在“Application\Home\View”视图文件夹中 创建一个文件夹"Index" (注意大小写),并在该文件夹中创建一个index.html的文件
![](https://i-blog.csdnimg.cn/blog_migrate/fbf5c795c8d94e01f303772db5f9f7b5.png)
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="{:U('upload')}" enctype="multipart/form-data" method="post">
<input type="file"name="photo" />
<input type="submit"value="导入数据">
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="{:U('upload')}" enctype="multipart/form-data" method="post">
<input type="file"name="photo" />
<input type="submit"value="导入数据">
</body>
</html>
第七步:修改Application\Home\Controller目录下的IndexController.class.php文件
![](https://i-blog.csdnimg.cn/blog_migrate/e6c45e09c01fb10c2a5c02790d62a1d8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fcca4915c278306afa88925b52124ab9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a00c1723012e0d711cb5fce5b95937cc.png)
代码如下:
<?php
namespace Home\Controller;
use Think\Controller;
use PHPExcel_IOFactory;
use PHPExcel;
use Behavior;
class IndexController extends Controller {
public function index(){
$this->display();
}
public function upload() {
namespace Home\Controller;
use Think\Controller;
use PHPExcel_IOFactory;
use PHPExcel;
use Behavior;
class IndexController extends Controller {
public function index(){
$this->display();
}
public function upload() {
set_time_limit(0);
ini_set('memory_limit','1024M');
if (!empty($_FILES)) {
$config = array(
'exts' => array('xlsx','xls'),
'maxSize' => 3145728000,
'rootPath' =>"./Public/",
'savePath' => 'Uploads/',
'subName' => array('date','Ymd'),
);
$upload = new \Think\Upload($config);
if (!$info = $upload->upload()) {
$this->error($upload->getError());
}
vendor("PHPExcel.PHPExcel");
$file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式
if ($extension == 'xlsx') {
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
} else if ($extension == 'xls'){
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
}
$sheet =$objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();//取得总行数
$highestColumn =$sheet->getHighestColumn(); //取得总列数
D('pro_info')->execute('truncate table pro_info');
for ($i = 2; $i <= $highestRow; $i++) {
//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置
$data['pId'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$data['pName'] =$objPHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();
$data['pPrice'] =$objPHPExcel->getActiveSheet()->getCell("C" .$i)->getValue();
$data['pCount'] = $objPHPExcel->getActiveSheet()->getCell("D". $i)->getValue();
//看这里看这里,这个位置写数据库中的表名
D('pro_info')->add($data);
}
$this->success('导入成功!');
} else {
$this->error("请选择上传的文件");
}
}
}
ini_set('memory_limit','1024M');
if (!empty($_FILES)) {
$config = array(
'exts' => array('xlsx','xls'),
'maxSize' => 3145728000,
'rootPath' =>"./Public/",
'savePath' => 'Uploads/',
'subName' => array('date','Ymd'),
);
$upload = new \Think\Upload($config);
if (!$info = $upload->upload()) {
$this->error($upload->getError());
}
vendor("PHPExcel.PHPExcel");
$file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式
if ($extension == 'xlsx') {
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
} else if ($extension == 'xls'){
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
}
$sheet =$objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();//取得总行数
$highestColumn =$sheet->getHighestColumn(); //取得总列数
D('pro_info')->execute('truncate table pro_info');
for ($i = 2; $i <= $highestRow; $i++) {
//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置
$data['pId'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$data['pName'] =$objPHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();
$data['pPrice'] =$objPHPExcel->getActiveSheet()->getCell("C" .$i)->getValue();
$data['pCount'] = $objPHPExcel->getActiveSheet()->getCell("D". $i)->getValue();
//看这里看这里,这个位置写数据库中的表名
D('pro_info')->add($data);
}
$this->success('导入成功!');
} else {
$this->error("请选择上传的文件");
}
}
}
第八步:运行ThinkPHP,导入excel文件
![](https://i-blog.csdnimg.cn/blog_migrate/f0554db1fcf7f8fef9894aff9b2e912a.png)
excel文件内容(根据数据库来):
![](https://i-blog.csdnimg.cn/blog_migrate/f0ea0394a682c26dcd587514803f45b3.png)
数据库结构:
![](https://i-blog.csdnimg.cn/blog_migrate/8b9a1f2fb9c1189e743f9ad6f603be89.png)
最后----大功告成!!
![](https://i-blog.csdnimg.cn/blog_migrate/3d0c2a392b7330dfa73c773bfea620d0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e0f96737be09ca5f12f43c1442eecc0e.png)