您的位置:首页 > 编程语言 > PHP开发

PHP使用ocupload插件 一键上传并解析EXCEL

2017-12-22 17:30 344 查看
1、HTML

<button id="uploadAnalysisExcel" style="width: 90px;">导入xx</button>

2、javascript
<script src='js/jquery-2.0.3.min.js'></script>
<script src="js/jquery.ocupload-1.1.2.js"></script>

//调用OCUpload插件的方法 一键上传并解析excel
$("#uploadAnalysisExcel").upload({
action:"/procurementCarriageFeeImport.php?action=Upload", //表单提交的地址
name:"upFile",
onComplete:function (data) { //提交表单之后
var res = $.parseJSON(data);
alert(res.msg);
if(res.status == 1){
window.location.href = window.location;
}
},
onSelect: function() {
//当选择了文件后,关闭自动提交
this.autoSubmit=false;
//校验上传的文件名是否满足后缀为.xls或.xlsx
var regex =/^.*\.(?:xls|xlsx)$/i;
if(regex.test($("[name = '"+this.name()+"']").val())){
//通过校验
this.submit();
}else{
//未通过
alert("文件格式不正确,必须以.xls或xlsx结尾!");
}
}
});
3、PHP

//读取文件
if(!empty($_GET['action'])&&$_GET['action']=='Upload'){

$_import_type = array_flip($_import_type); //键值互换一下

$uploadFiles = $_FILES["upFile"]["tmp_name"]; //临时存储目录

$PHPExcel = new PHPExcel();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($uploadFiles)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($uploadFiles)){
'no Excel';
return ;
}
}
$val=array();//存放读取的数据
$PHPExcel = $PHPReader->load($uploadFiles);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();

/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
/**从第A列开始输出*/

for($currentColumn= 'A';$currentColumn<= 'C'; $currentColumn++){

$pronum = trim($currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue());/**ord()将字符转为十进制数*/
if(!empty($pronum)){

$val[$currentRow]['fee_info'][] = $pronum;
}else{
break;
}
}
}
if(count($val) > 0){
//TODO:
echo json_encode(array('status'=>1,'msg'=>' 导入成功!'));
}else{
echo json_encode(array('status'=>0,'msg'=>' 导入失败,采购单号'.rtrim($_not_exist_po,',').'不存在!'));
}
die;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 插件 excel