zoukankan      html  css  js  c++  java
  • ThinkPHP中使用PHPExcel导入Excel

    下载PHPEXCEL 后放到项目里。

     public function InExcel() {
            //设定缓存模式为经gzip压缩后存入cache(PHPExcel导入导出及大量数据导入缓存方式的修改 ) 
            $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
            $cacheSettings = array();
            PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
    
            $objPHPExcel = new PHPExcel();
            //读入上传文件
            if ($_POST) {
                $objPHPExcel = PHPExcel_IOFactory::load($_FILES["inputExcel"]["tmp_name"]);
                //内容转换为数组 
                $indata = $objPHPExcel->getSheet(0)->toArray();
                //print_r($indata);
                //excel  sheet个数
                //echo $objPHPExcel->getSheetCount();
                //把数据新增到mysql数据库中
                $arr = array();
                $j = 0;
                $model = D('goods_unit');
                //根据具体情况修改
                if (count($indata) == 1) {
                    //根据具体情况修改
                    $arr[0] = array("hang" => 0, "Cause" => "无记录!");
                    $this->assign('errorss', $arr);
                    $list = $this->menu();
                    $this->assign('menu', $list);
                    $this->display('Base:baseunit', 'utf-8');
                } else {
                    if ($indata[0][0] == "单位名称") {
                        for ($i = 1; $i < count($indata); $i++) {
                            $all['UnitName'] = $indata[$i][0];
                            $w = $model->where($all)->select();
                            if ($w !== null) {
                                $arr[$j] = array("hang" => $i + 1, "Cause" => "该单位已存在");
                                $j++;
                                continue;
                            }
                            $result = $model->add($all);
                            if (false == $result) {
                                $arr[$j] = array("hang" => $i + 1, "Cause" => "写入单位表时失败");
                                $j++;
                                continue;
                            }
                        }
                        if ($arr == null) {
                            $arr[0] = array("hang" => 0, "Cause" => "导入Excel成功");
                        }
                        $this->assign('errorss', $arr);
                        $list = $this->menu();
                        $this->assign('menu', $list);
                        $this->display('Base:baseunit', 'utf-8');
                    } else {
                        $arr[0] = array("hang" => 0, "Cause" => "上传文件格式不正确!");
                        $this->assign('errorss', $arr);
                        $list = $this->menu();
                        $this->assign('menu', $list);
                        $this->display('Base:baseunit', 'utf-8');
                    }
                }
            } else {
                $arr[0] = array("hang" => 0, "Cause" => "服务器未检测到有上传文件!");
                $this->assign('errorss', $arr);
                $list = $this->menu();
                $this->assign('menu', $list);
                $this->display('Base:baseunit', 'utf-8');
            }
        }

      以下是前台代码

    <script> 
       $("#ProjectToolbar").on('click', "a[name='haveExcel']", function(data) {
            $w= $('#mk').html().trim();
            if($w=="选择上传文件"){
                $("#inputFile").click();
            }else{
                if($('#inputFile').val()!=null){
                    $("#submitexcel").click();
                }else{
                    alert("还未选择文件");
                    $('#mk').html("选择上传文件");
                }
            }
        });
              
        function dohtml() { 
            $w= $('#mk').html().trim();
            if($w=="选择上传文件"){
                $('#mk').html("上传所选文件");
            }else{
                $('#mk').html("选择上传文件");
            }
        }
    </script>
    <div id="ProjectToolbar">
       <form action="__URL__/InExcel" id="formExcel" method="post" enctype="multipart/form-data"> 
            <input type="file" name="inputExcel" id="inputFile" style="display:none;"  onchange="dohtml()">
            <a name="haveExcel" id="mk"  href="javascript:;">选择上传文件</a>
            <input type="submit" name="submite" id="submitexcel" value="提交"  style="display:none;" />
       </form> 
    </div>
    

      用ajax不知道怎么做。

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood (贪心)
    HDU 2544 最短路 (Floyd)
    CodeForces 632C Grandma Laura and Apples (模拟)
    CodeForces 731F Video Cards (数论+暴力)
    CodeForces 731C Socks (DFS或并查集)
    CodeForces 731B Coupons and Discounts (水题模拟)
    CodeForces 731A Night at the Museum (水题)
    UVaLive 6834 Shopping (贪心)
    zzuli 1484 继续双线
    zzuli 1875多线DP
  • 原文地址:https://www.cnblogs.com/aroundight/p/3718450.html
Copyright © 2011-2022 走看看