thinkphp导入excel到mysql_ThinkPHP3.2.3+PHPExcel1.8.0导入表格到数据库

代码修改成如下:

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

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(); //取得总列数

for ($i = 2; $i <= $highestRow; $i++) {

//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置

$data['a'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();

$data['b'] =$objPHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();

$data['c'] =$objPHPExcel->getActiveSheet()->getCell("C" .$i)->getValue();

$data['d'] = $objPHPExcel->getActiveSheet()->getCell("D". $i)->getValue();

$data['e'] =$objPHPExcel->getActiveSheet()->getCell("E" .$i)->getValue();

$data['f'] =$objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();

$data['input_time'] = time();

$data['promotion_type'] = mt_rand(3, 5);

//看这里看这里,这个位置写数据库中的表名

M('goods')->add($data);

}

$this->success('导入成功!');

} else {

$this->error("请选择上传的文件");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值