天天看点

ajax上传excel php,jQuery+php+ajax+PHPExcel实现上传excel文件导入数据库

项目中需要批量导入数据,感觉这个需求以后也会经常用,必须总结分享下:

引入jquery的第三方表单插件:

视图文件:goods_list.ctp(商品列表),

批量导入

.btnimport{position: relative;overflow: hidden;margin-right: 4px;display:inline-block; *display:inline;padding:4px 10px 4px;font-size:14px;line-height:18px; *line-height:20px;color:#fff; text-align:center;vertical-align:middle;cursor:pointer;background:#5bb75b; border:1px solid #cccccc;background-color: #1fb5ad;; border-bottom-color:#b3b3b3;-webkit-border-radius:4px; border-color: #1fb5ad; -moz-border-radius:4px;border-radius:4px; padding: 5px 10px; font-size: 12px; line-height: 1.5; border-radius: 3px; }

.btnimport input{position: absolute;top: 0; right: 0;margin: 0;border:solid transparent; opacity: 0;filter:alpha(opacity=0); cursor: pointer;}

使用input样子太丑了,还有个长条框,于是添加CSS修改样子做的和其他按钮样子一样。每个上传对应一个form表单,多个商品对应各自的上传事件,于是在form中添加id="form_<?php echo $good['Good']['id']?>",这样每个商品的导入就可以调用各自的ajaxSubmit事件。

ajax上传excel php,jQuery+php+ajax+PHPExcel实现上传excel文件导入数据库

传入的jquery, 使用ajaxsubmit来提交表单。Jquery表单插件ajaxForm用法详解

$(function () {

$(".fileupload").change(function(){

var btnimport = $(".btnimport span");

var id = $(this).data('id');

$("#form_" + id).ajaxSubmit({

dataType: 'json',

data:{id:id},

success: function(data) {

switch(data['code']){

case 0:{

alert("批量导入成功");

//btnimport.html("批量导入"); //按钮还原

window.location.reload();

break;

}

case -6000:{

btnimport.html("上传失败");

alert("上传失败!");

break;

}

case -6001:{

btnimport.html("上传失败");

alert("文件格式不正确!");

break;

}

default:{

alert("系统繁忙,请稍后再试!");

break;

}

}

},

error:function(xhr){

btnimport.html("上传失败");

}

});

});

});

action中的处理方法:

public function batchImport(){

$id = $this->request->data('id');

$DOMAIN = $_SERVER['HTTP_HOST'];

$file = $_FILES;

$filename = $file['importExcel']['name'];

$file_temp_name =$file['importExcel']['tmp_name'];

$dir = WWW_ROOT.'/files' . DS . 'xls';

$type = strstr($filename,'.');

if($type != '.xls' && $type != '.xlsx'){

$this->_err_ret(-6001,'sys err');

}

if(is_uploaded_file($file_temp_name)){

$full_name = $dir.DS. date ( 'Y-m-d_h:i:s' ).'_'.$filename;

$result = move_uploaded_file($file_temp_name, $full_name);

//处理文件路径,便于访问

//$full_name = explode('webroot/', $full_name);

//$full_name = 'http://' . $DOMAIN .'/aaa/bbb'.$full_name[1];

}else{

$this->_err_ret(-6000,'err');

}

//如果上传文件成功,就执行导入 excel操作

$objReader = PHPExcel_IOFactory::createReaderForFile($full_name);

$objPHPExcel = $objReader->load($full_name);

$objPHPExcel->setActiveSheetIndex(0);

$sheet = $objPHPExcel->getSheet(0);

$highestRow = $sheet->getHighestRow(); // 取得总行数

$test = $objPHPExcel->getActiveSheet()->getCell('A2')->getValue();

$data = array();

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

$sn = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();

$pwd = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getValue();

$this->Card->create();

$data = array(

'sn' => $sn,

'pwd' => $pwd,

'good_id' => $id,

);

if(!$this->Card->save($data)){

$this->_err_ret(-6000,'err');

exit;

}

}

$newNumber = (int)$this->Good->findById($id)['Good']['number']+(int)$highestRow-1;

$this->Good->id = $id;

if(!$this->Good->saveField('number',$newNumber)){

$this->_err_ret(-6000,'err');

}

$this->_suc_ret($id);

}

PHPExcel实现上传excel文件导入数据库

项目中需要批量导入数据,感觉这个需求以后也会经常用,必须总结分享下: 引入jquery的第三方表单插件:

js上传Excel文件

一.问题 需要在项目里添加一个上传excel文件的功能,因为其他同样的后台里面有上传文件的功能,第一反应就是想着直接用.了解了一下发现它是利用bootstrap的fileinput实现的,但是我怎么都 ...

java上传excel文件及解析

java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...

Django框架(上传Excel文件并读取)

博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...

使用phpExcel批量上传excel表数据到mysql数据库中

if(isset($_POST['submit']) && $_POST['submit']=='上传文件') { //导入类文件 require_once (& ...

上传excel数据到数据库中

上传excel表格数据到数据库 导入固定路径下的excel数据到数据库

vue&period;js学习 自定义过滤器使用(2)

gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...

Declaration Merging with TypeScript

原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/ Why might you need this? There can ...