请先阅读以下步骤再到结尾下载源码
第一步:下载 thinkphp_3.2.3 和 PHPExcel_1.8.0 并解压
对应的网站分别为:
http://www.thinkphp.cn/down.html
https://github.com/PHPOffice/PHPExcel
第二步:
①将解压出来的ThinkPHP文件拷贝到网站根目录
②并创建index.php 代码如下
第三步:
①在ThinkPHP\Library\Vendor目录下新建文件夹PHPExcel
②将PHPExcel_1.8.0_doc\Classes中的文件拷贝到ThinkPHP\Library\Vendor\
PHPExcel中
PHPExcel中
第四步:
①运行根目录的index.php文件,并手动创建Public文件夹
ThinkPHP欢迎界面:
根目录结构:
②Application目录如下:
第五步:ThinkPHP连接我们的mysql数据库
文件位置 : 根目录/Application/Home/Conf/config.php
第六步:在“Application\Home\View”视图文件夹中 创建一个文件夹"Index" (注意大小写),并在该文件夹中创建一个index.html的文件
代码:
<!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文件
代码如下:
<?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() {
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("请选择上传的文件");
}
}
}
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() {
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文件
excel文件内容(根据数据库来):
数据库结构:
最后----大功告成!!
##源码下载地址
https://blog.csdn.net/yssong1028/article/details/75913317
评论(0)