PHP用Excel模拟表单上传数据保存到MYSQL

PHP用Excel模拟表单上传数据保存到MYSQL_第1张图片
黑科技.png
1、用EXCEL来进行数据的传输,首先需要下载一个插件PHPExcel。可以到官网上下载。
2、思路总结:

用Excel进行上传,再把数据保存到数据库,一般情况下没有大的问题,但是有的表可能牵扯到多个表和很多算法,简单粗暴的直接导入,会让很多数据没发衔接,没有实际的作用。这里还要进行的操作是进行模拟提交。思路大概就是这样。

3、代码操作如下:

PHP:

    header("content-type:text/html;charset=utf-8");
    set_time_limit(0);
    function read($filename,$encode='utf-8'){
            include_once('./PHPexcel/PHPExcel.php');
            $objReader = PHPExcel_IOFactory::createReader('Excel5');
            $objReader->setReadDataOnly(true);
            $objPHPExcel = $objReader->load($filename);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
            $highestColumn = $objWorksheet->getHighestColumn();
            $highestColumnIndex =    PHPExcel_Cell::columnIndexFromString($highestColumn);
            $temp  = array();
            for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
                for ($column = 0; $column < $highestColumnIndex; $column++) {//列数是以A列开始
                    $temp[$row][] =$objWorksheet->getCellByColumnAndRow($column,$row)->getValue();
        }
            }
        return $temp;
    }

HTML:


    

PHP:

if(!empty($_POST['filename'])){
        $filename = trim($_POST['filename']);
        $extension = substr(strrchr($filename, '.'), 1);
        if($extension != 'xls'){
            exit('文件格式错误');
        }
        if(!file_exists($filename)){
            exit('文件不存在');
        }
        $conn = mysqli_connect('localhost','root','') or die('连接失败');
        mysqli_select_db($conn,'wgw2');
        mysqli_set_charset($conn,'utf8');
        $a = read($filename);
        $str = '';
        $i = 0;
        $j = 0;
        foreach($a as $k=>$v){
        if($k==1) continue;
            $arr = array();
            $arr['cateid'] = $v[0];
            $arr['brand'] = $v[1];
            $arr['sgid'] = $v[2];
            $arr['title'] = $v[3];
            $arr['title2'] = empty($v[4]) ? '' : $v[4];
            $arr['title_style_color'] = empty($v[5]) ? '' : $v[5];
            $arr['title_style_bold'] = empty($v[6]) ? '' : $v[6];
            $arr['keywords'] = empty($v[7]) ? '' : $v[7];
            $arr['description'] = empty($v[8]) ? '' : $v[8];
            $arr['money'] = $v[9];
            $arr['yunjiage'] = $v[10];
            $arr['maxqishu'] = $v[11];
            $arr['thumb'] = $v[12];
            $arr['uppicarr'] = empty($v[13])? null : json_encode(explode('|',$v[13]));
            $arr['goods_key'] = json_encode(array('pos' => $v[14],'renqi' => $v[15]));
            $arr['xsjx_time'] = $v[16];
            $arr['xsjx_time_h'] = $v[17];
            $arr['content'] = str_replace("'",'"',$v[18]);
            if ($v[9]< 30) {
            }else{
                if ($v[9] >50) {
                    $arr['money'] = $v[9] + 5;
                }else{
                    $arr['money'] = $v[9] * 1.2;
                }
                $bb = http_request('http://yungou.d8qb.com/index.php/admin/content/goods_add',$arr);
                    $reg = "/商品添加成功/";
            if(preg_match($reg,$bb)){
                $j++;
            }else{
                $str .= $k . ',';
            }
            $i++;
            }
        }
        echo '导入完成';
        echo '
'; echo '共'.$i.'个'; echo '
'; echo '成功'.$j.'个'; echo '
'; echo '失败编号'.$str;

你可能感兴趣的:(PHP用Excel模拟表单上传数据保存到MYSQL)