PHP导Excel文件例子例一

这篇博客展示了如何使用PHP处理Excel文件,包括创建控制器UploadController.php和设计视图index.blade.php,还提供了一个Excel模板文件作为示例。
摘要由CSDN通过智能技术生成

视图index.blade.php

<style type="text/css">
    .td_class{
   
        width:80px !important;
        background: none;
        text-align: center;
    }
    .td_parent{
   
        width:100px !important;
    }
    .none_input{
   
        border: none;
        outline: none;
    }
    .th_class{
   
        padding: 10px;
        border: 1px solid #dedada;
    }
    .bold{
   
        font-weight:bold;
    }
</style>
<link rel="stylesheet" href="/layui-v2.5.6/layui/css/layui.css"  media="all">
<div class="content" style="min-height:0px;">
    @if(isset($is_explort) && $is_explort == 1)
    <div class="row" style="">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">导入模块</div>
                <div class="panel-body">
                    <div class="form-group">
                        <div class="col-md-2">
                            <div class="col-sm-12">
                                <input type="text" name="showbtn" class="form-control showbtn" readonly="" value="" required="1" />
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label for="add_files" class="btn btn-default" style="">导入文件</label>
                            <input name="add_files" style="display:none" type="file" class="btn btn-sm btn-default add_files" id="add_files">
                    
                            <label for="add_files_sub" class="btn btn-default" style="">提取</label>
                            <input name="add_files_sub" style="display:none" type="text" class="btn btn-sm btn-default add_files_sub" id="add_files_sub">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif
</div>

<!-- 请稍等模态框 -->
<div class="modal fade" id="loading" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">提示</h4>
            </div>
            <div class="modal-body">
                请稍候。。。<span id="result"></span>
            </div>
        </div>
    </div>
</div>
<!-- 请稍等模态框 -->
<script>
    // 导入数据
    var sub_src = '';
    $('.add_files_sub').unbind('click').click(function() {
   
        var formData = new FormData();
        formData.append('file',sub_src);
        formData.append('_token',LA.token);
        $('#loading').modal('show');
        var aj = $.ajax({
   
            url: '/admin/pat_online_track/addTrackFiles',
            type: 'post',
            data: formData,
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            dataType:'json',
            success: function (res) {
   
                if (res.code == 1) {
   
                    window.location.reload()
                }else{
   
                    swal(res.msg, '', 'error');
                }
            }
        });
        $.when(aj).done(function (e) {
   
            $('#loading').modal('hide');
        });
    });
    $('.add_files').change(function(){
   
        sub_src = $('.add_files')[0]['files'][0]
        $(".showbtn").val($(".add_files")[0].files[0]['name'])
    });
</script>

控制器

// 导入表格
    public function addTrackFiles(Request $request)
    {
   
        $files = $request->file;//excel文件

        if(!$files || $files == 'undefined') {
   
            return response()->json(['code' => 0,'msg' => '获取失败,文件不能为空']);
        }
        $data = UploadController::upload_path_file($files,'tmp');
        $datas = $this->init_excel('./'.$data,2);

        if (!$datas) {
   
            return response()->json(['code' => 0,'msg' => '数据提取为空,请检查']);
        }
        $set_data = $this->getData($datas);
        if (isset($set_data['arr']) && $set_data['arr']) {
   
            DB::beginTransaction();
            try {
   
                $main_arr = [];
                $main_arr['factory_name'] = $set_data['factory_name']['factory_name']??"";
                if (isset($main_arr['factory_name']) && $main_arr['factory_name']) {
   
                    foreach (factory_abbreviation() as $key => $value) {
   
                        if ($value == $main_arr['factory_name']) {
   
                            $main_arr['factory_name'] = $key;
                        }
                    }
                }

                if (!is_numeric($main_arr['factory_name'])) {
   
                    return response()->json(['code' => 0,'msg' => '未找到匹配工厂,请检查工厂名称']);
                }
                $main_arr['factory_time'] = 1;
                $main_arr['created_at'] = getDateH(0); 
                $main_id = DB::table("pat_online_strack")->insertGetId($main_arr);
                foreach ($set_data['arr'] as $key => $value) {
   
                    if ($value) {
   
                        $insert_arr = [];

                        $insert_arr['factory_id'] = $main_arr['factory_name']??"";
                        $insert_arr['pat_online_strack_id'] = $main_id;
                        $insert_arr['team_name'] = $value[0]??"";
                        $insert_arr['team_num'] = $value[1]??"";
                        $insert_arr['client_name'] = $value[2]??"";
                        $insert_arr['expliot_no'] = $value[3]??"";
                        $insert_arr['order_no'] = $value[4]??"";
                        $insert_arr['product_name'] = $value[5]??"";
                        $insert_arr['color_name'] = $value[6]??"";
                        $insert_arr['num'] = $value[7]??0;
                        $insert_arr['price'] = $value[8]??0;

                        if ($insert_arr['num'] && $insert_arr['p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值