Thinkphp5.1上传Excel

1,controller :\app\项目名称\controller\Ai.php
public function show_import_web(){
$this->fetch = true;
}
2,view:\app\项目名称\view\Ai\show_import_web.html1
1
< type=“text/javascript” src="/files/layui/layui.js">

<legend>数据导入</legend>
 <legend>二、上传Excel文件</legend>
 </fieldset>
    <button type="button" class="layui-btn layui-btn-primary" id="update-excel-btn">只允许Excel文件</button>
</div>
<div class="layui-form-item">
    <div class="layui-input-block">
   <button class="layui-btn" lay-submit="" lay-filter="import_excel_btn">立即提交</button>
   </div>
</div>

< script language=“javascript”>
layui.use([‘form’, ‘upload’], function(){
var form = layui.form,upload = layui.upload;
//监听提交
form.on(‘submit(import_excel_btn)’, function(data){
// layer.msg(‘正在提交处理,请稍候…’, { icon: 16,time: 100000,shade : [0.5 , ‘#000’ , true] });
});
upload.render({ //允许上传的文件后缀
elem: ‘#update-excel-btn’
,url: ‘/lawyer/ai/upload_ai_data’
,accept: ‘file’ //普通文件
,exts: ‘xls|xlsx|csv’ //只允许上传excel文件
,done: function(res){
if(res.code==0){
layer.alert(res.msg);
}else{
layer.alert(res.msg);
}
}
});
});
< /script>
3,controller :\app\项目名称\controller\Ai.php
public function upload_ai_data(){
include ROOT_PATH . ‘include’ . DS . ‘PHPExcel-1.8.0’ . DS . ‘PHPExcel.php’;
$file = request()->file(‘file’);
$info = f i l e > m o v e ( . . / . . / f i l e . . c o m ) ; i f ( file->move( '../../file.***.com'); if( info){
//获取上传到后台的文件名
$fileName = $info->getSaveName();
//获取文件后缀
$suffix = i n f o > g e t E x t e n s i o n ( ) ; / / i f ( info->getExtension(); //判断哪种类型 if( suffix == “xlsx”){
$reader = \ =PHPExcel_IOFactory::createReader(‘Excel2007’);
}else{
$reader = \PHPExcel_IOFactory::createReader(‘Excel5’);
}
}else{
$this->error(‘文件过大或格式不正确导致上传失败-_-!’);
}
$get_path_name = $info->getPathname();
$excel = r e a d e r > l o a d ( " reader->load(" get_path_name",$encode = ‘utf-8’);
$sheet = $excel->getSheet(0);
$row_num = $sheet->getHighestRow();
//获取总列数
$col_num = $sheet->getHighestColumn();
c o l u m = a r r a y ( ) ; f o r ( colum = array(); for ( i=0; i < = 6 ; i<=6; i++){
$s = s h e e t > g e t C e l l B y C o l u m n A n d R o w ( sheet->getCellByColumnAndRow( i,1)->getValue();
$colum[] = KaTeX parse error: Expected 'EOF', got '}' at position 12: s; }̲ if(colum[0] == “***” && $colum[1] == “***” && $colum[2] == “***” && $colum[3] == “***” && $colum[4] == “***” && $colum[5] == "**"){
c r e a t e t i m e = d a t e ( Y m d H : i : s , createtime = date('Y-m-d H:i:s', this->now);
$userinfo = helper(‘Auth’)->user();
$lawyerid = $userinfo[‘Member’][‘user_id’];
$data = []; //数组形式获取表格数据
s u m = 0 ; f o r ( sum = 0; for ( i = 2; $i <= $row_num; $i++) {
$data[‘question’] = s h e e t > g e t C e l l ( " A " . sheet->getCell("A". i)->getValue();
$data[‘sex’] = s h e e t > g e t C e l l ( " B " . sheet->getCell("B". i)->getValue();
$data[‘marriage’] = e x c e l > g e t A c t i v e S h e e t ( ) > g e t C e l l ( " C " . excel->getActiveSheet()->getCell("C". i)->getValue();
$data[‘raise’] = e x c e l > g e t A c t i v e S h e e t ( ) > g e t C e l l ( " D " . excel->getActiveSheet()->getCell("D". i)->getValue();
$data[‘assets’] = e x c e l > g e t A c t i v e S h e e t ( ) > g e t C e l l ( " E " . excel->getActiveSheet()->getCell("E". i)->getValue();
$data[‘house_property’] = e x c e l > g e t A c t i v e S h e e t ( ) > g e t C e l l ( " F " . excel->getActiveSheet()->getCell("F". i)->getValue();
$data[‘debt’] = e x c e l > g e t A c t i v e S h e e t ( ) > g e t C e l l ( " G " . excel->getActiveSheet()->getCell("G". i)->getValue();
if( $data[‘sex’] == “” || $data[‘sex’] == NULL){
$data[‘sex’] = 0;
}
if( $data[‘marriage’] == “” || $data[‘sex’] == NULL){
$data[‘marriage’] = 0;
}
if( $data[‘raise’] == “” || $data[‘sex’] == NULL){
$data[‘raise’] = 0;
}
if( $data[‘assets’] == “” || $data[‘sex’] == NULL){
$data[‘assets’] = 0;
}
if( $data[‘house_property’] == “” || $data[‘sex’] == NULL){
$data[‘house_property’] = 0;
}
if( $data[‘debt’] == “” || $data[‘sex’] == NULL){
$data[‘debt’] = 0;
}
q t = D b : : c o n n e c t ( qt = Db::connect( this->_db_config)->table(‘fa_ai_question’)->field(‘id’)->where([
‘question’ => d a t a [ q u e s t i o n ] ] ) > s e l e c t ( ) ; i f ( ! data['question'] ])->select(); if(! qt){
//将数据保存到数据库
r e s = D b : : c o n n e c t ( res = Db::connect( this->_db_config)->table(‘fa_ai_question’)->insert([‘question’ => $data[‘question’], ‘lawyerid’ => l a w y e r i d , c r e a t e t i m e = > lawyerid,'createtime'=> createtime, ‘sex’ => $data[‘sex’],‘marriage’ => $data[‘marriage’],‘raise’ => $data[‘raise’],‘assets’ => $data[‘assets’], ‘house_property’ => $data[‘house_property’], ‘debt’ => d a t a [ d e b t ] ] ) ; i f ( data['debt']]); if( res){
$res = 0;
}else{
$res = 1;
}
}
$sum += KaTeX parse error: Expected 'EOF', got '}' at position 18: …s; }̲ if…sum == 0){
r e t = [ " c o d e " = > 0 , " m s g " = > " " , " d a t a " = > [ ] ] ; e c h o ( j s o n e n c o d e ( ret = ["code"=>0, "msg"=>"上传成功", "data"=>[] ]; echo(json_encode( ret));
exit;
}else{
r e t = [ " c o d e " = > 0 , " m s g " = > " " , " d a t a " = > [ ] ] ; e c h o ( j s o n e n c o d e ( ret = ["code"=>0, "msg"=>"", "data"=>[] ]; echo(json_encode( ret));
exit;
}
}else{
r e t = [ " c o d e " = > 1 , " m s g " = > " " , " d a t a " = > [ ] ] ; e c h o ( j s o n e n c o d e ( ret = ["code"=>1, "msg"=>"格式不正确", "data"=>[] ]; echo(json_encode( ret));
exit;
}
}
/仅供参考
/

发布了58 篇原创文章 · 获赞 0 · 访问量 2778

猜你喜欢

转载自blog.csdn.net/qq_30439399/article/details/100727410