excel文件上传与导入

1.文件上传
//上传按钮隐藏
<style>
#fileid_auth_image7 {
    position: absolute;
    left: 0;
    width: 100%;
    height: 90px;
    top: 80px;
    opacity: 0
}
</style>
//上传按钮
<label><button type="button" class="btn yellow btn-sm" name="option" onclick="img()"> <i class="fa fa-html5"></i>  {fc_lang('导入数据')}</button></label>
 <input type="file" name="img7" id="fileid_auth_image7" class="ajax_file_upload7" accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel">
 <input type="hidden" class="cover7 i_check" id="cover7"/>
2.上传图片的js
<script>
        
    $("#fileid_auth_image7").change(function(){
        var fileElementId = $(this).attr('id'); //input id
        var name = $(this).attr('name');//input name
        $.ajaxFileUpload({   
            url:'/index.php?s=member&c=api&m=ajax_upload&fname='+name,
        secureuri:false,   
            fileElementId:fileElementId,
            dataType: 'json',              
            success: function (data) {
               
                if(data.code == 0){
                    alert(data.msg);
                }else{
                    $(".cover7").attr('value',data.id);
                    //提交数据
                    $.get('index.php?c=diy&m=import_excel&id='+data.id);
                    alert('导入成功');
                    setTimeout('window.location.href = "/admin.php?s=mall&c=home&m=index"', 2000);
                }
               
            },
            error: function (data, status, e){
                $("#"+fileElementId).attr('value',data.id);
                //img.attr('src',data.url);
                console.log(data);
                $("#dr_row_auth_image7").attr('src',data.url);
            }   
        });
    })
        
        function img (){
            $("#fileid_auth_image7").click();
        };
        
</script>
3.上传的接口
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Api extends M_Controller {
    // ajax 图片上传
    public function ajax_upload() {
        // 游客不允许上传
        !$this->member && exit(json_encode(array('code'=>0, 'msg'=>fc_lang('游客不允许上传附件'), 'url'=> '', 'id'=>'')));
        // 会员组权限
        $member_rule = $this->get_cache('member', 'setting', 'permission', $this->member['mark']);
        // 是否允许上传附件
        !$this->member['adminid'] && !$member_rule['is_upload'] && exit(json_encode(array('code'=>0, 'msg'=>fc_lang('您的会员组无权上传附件'), 'url'=> '', 'id'=>'')));
        // 附件总大小判断
        if (!$this->member['adminid'] && $member_rule['attachsize']) {
            $data = $this->db->select_sum('filesize')->where('uid', $this->uid)->get('attachment')->row_array();
            $filesize = (int)$data['filesize'];
            $filesize > $member_rule['attachsize'] * 1024 * 1024 && exit(json_encode(array('code'=>0, 'msg'=>fc_lang('附件空间不足!您的附件总空间%s,现有附件%s。', $member_rule['attachsize'].'MB', dr_format_file_size($filesize)), 'url'=> '', 'id'=>'')));
        }
        $ext = 'jpg,jpeg,gif,png,csv';
        $site = (int)$this->input->get('siteid');
        $site = $site ? $site : SITE_ID;
        $code = str_replace(' ', '+', $this->input->get('code'));
        list($size, $path) = explode('|', dr_authcode($code, 'DECODE'));
        $path = $path ? SYS_UPLOAD_PATH.'/'.$path.'/' : SYS_UPLOAD_PATH.'/'.date('Ym', SYS_TIME).'/';
        !is_dir($path) && dr_mkdirs($path);
        $this->load->library('upload', array(
            'max_size' => (int)$size * 1024,
            'overwrite' => FALSE,
            'file_name' => substr(md5(time()), rand(0, 20), 10),
            'upload_path' => $path,
            'allowed_types' => str_replace(',', '|', $ext),
            'file_ext_tolower' => TRUE,
        ));
        
        if ($this->upload->do_upload(isset($_GET['fname']) ? $_GET['fname'] : 'Filedata')) {
            $info = $this->upload->data();
            $this->load->model('attachment_model');
            $this->attachment_model->siteid = $site;
            $result = $this->attachment_model->upload($this->uid, $info);
            !is_array($result) && exit('0,'.$result);
            list($id, $file, $_ext) = $result;
            echo json_encode(array('code'=>1, 'msg'=>'', 'name' => dr_strcut($info['filename'], 15).'.'.$_ext, 'id'=>$id, 'url' => dr_get_file($id)));exit;
        } else {
            echo json_encode(array('code'=>0, 'msg'=>$this->upload->display_errors('', ''), 'url'=> '', 'id'=>''));exit;
        }
    }
    
}
4.excel导入功能
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/* v3.1.0  */
 
class Diy extends M_Controller {
    //导入功能
    public function import_excel($id){
        $id = $this->input->get('id');
        $file_name = dr_get_file($id);
        
    $f = fopen($file_name, 'r');
    
    $columns = fgetcsv($f);
    
    while ($row = fgetcsv($f)) {
        $row = array_combine($columns, $row);
        $data = [
            'title' =>  trim(iconv('gb2312','utf-8',$row['title'])),
            'thumb' => '',
            'keywords'=>'',
            'description'=>'',
            'hits'=>'',
            'uid'=>1,
            'author' => 'mutaoinc',
            'status'=>'9',
            'url'=>'',
            'link_id'=>'',
            'tableid'=>'',
            'inputip'=>$this->input->ip_address(),
            'inputtime'=>SYS_TIME,
            'updatetime'=>SYS_TIME,
            'comments'=>'',
            'favorites' => 0,
            'displayorder' => 0,
            'order_price'=>trim(iconv('gb2312','utf-8',$row['order_price'])),
            'order_quantity'=>trim(iconv('gb2312','utf-8',$row['order_quantity'])),
            'order_volume'=>trim(iconv('gb2312','utf-8',$row['order_volume'])),
            'order_specification'=>'',
            'order_shipping'=>'',
            'order_shipping_param'=>'',
            'order_city'=>'',
            'order_status'=>'',
            'order_sn'=>trim(iconv('gb2312','utf-8',$row['order_sn'])),
            'cpbh'=>trim(iconv('gb2312','utf-8',$row['cpbh'])),
            'biaozhunzhi'=>trim(iconv('gb2312','utf-8',$row['biaozhunzhi'])),
            'cashao'=>trim(iconv('gb2312','utf-8',$row['cashao'])),
            'guige'=>trim(iconv('gb2312','utf-8',$row['guige'])),
            'huoqi'=>trim(iconv('gb2312','utf-8',$row['huoqi'])),
            'youxiaoqi'=>trim(iconv('gb2312','utf-8',strtotime($row['youxiaoqi']))),
            'yanzhidanwei'=>trim(iconv('gb2312','utf-8',$row['yanzhidanwei'])),
            'biaozhunjia'=>trim(iconv('gb2312','utf-8',$row['biaozhunjia'])),
            'zhuanshujia'=>trim(iconv('gb2312','utf-8',$row['zhuanshujia'])),
            'goods_ename'=>trim(iconv('gb2312','utf-8',$row['goods_ename'])),
            'picihao'=>trim(iconv('gb2312','utf-8',$row['picihao'])),
            'pinpai'=>trim(iconv('gb2312','utf-8',$row['pinpai'])),
            'jinjia'=>trim(iconv('gb2312','utf-8',$row['jinjia'])),
            'zhiliangnongdu'=>trim(iconv('gb2312','utf-8',$row['zhiliangnongdu']))
            ];
            
            //执行插入数据
        $insert_result = $this->db->insert('imt_1_mall', $data);
        //更新url数据
        $sql = "UPDATE imt_1_mall SET url = concat('/index.php?s=mall&c=show&id=',id)";
            $update_result = $this->db->query($sql);
    }
}
}
有帮助(- 没帮助(-