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

来源:互联网 发布:数据结构和算法分析pdf 编辑:程序博客网 时间:2024/05/21 09:09

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;}


阅读全文
0 0
原创粉丝点击