网站推广.NET

网站推广.NET

剑灵捏人数据怎么导入 PHP导入Execl表到数据库

来源:互联网

php导入execl表到数据库

/**     * 上传文件     */    function uploadFileforExcel()    {        // 允许上传的图片后缀        //$allowedExts = array("gif", "jpeg", "jpg", "png","xls");        $allowedExts = array("xls", "xlsx");        $temp = explode(".", $_FILES["file"]["name"]);        echo $_FILES["file"]["size"];        $extension = end($temp);     // 获取文件后缀名        if ($_FILES["file"]["size"]  0) {                echo "错误:: " . $_FILES["file"]["error"] . "<br>";                return "";            } else {                // 判断当期目录下的 upload 目录是否存在该文件                // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777                if (file_exists(dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . "uploadfile" . DIRECTORY_SEPARATOR . $_FILES["file"]["name"])) {                    echo $_FILES["file"]["name"] . " 文件已经存在。 ";                } else {                    // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下                    move_uploaded_file($_FILES["file"]["tmp_name"], dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . "uploadfile" . DIRECTORY_SEPARATOR . $_FILES["file"]["name"]);                    return dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . "uploadfile" . DIRECTORY_SEPARATOR . $_FILES["file"]["name"];                }            }        } else {            echo "非法的文件格式";            return "";//非法的文件格式        }    }    /**     * 获取Execl表格数据     */    function getExeclData()    {        //首先导入PhPExcel        require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/api/PHPExcel/Classes/PHPExcel.php');        $filePath = $this-&gt;uploadFileforExcel();        if ($filePath == null || $filePath == '') {            return;        }        //建立reader对象        $PHPReader = new PHPExcel_Reader_Excel2007();        if (!$PHPReader-&gt;canRead($filePath)) {            $PHPReader = new PHPExcel_Reader_Excel5();            if (!$PHPReader-&gt;canRead($filePath)) {                echo 'no Excel';                return;            }        }        //建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件        $PHPExcel = $PHPReader-&gt;load($filePath);        /**读取excel文件中的第一个工作表*/        $currentSheet = $PHPExcel-&gt;getSheet(0);        /**取得最大的列号*/        $allColumn = $currentSheet-&gt;getHighestColumn();        /**取得一共有多少行*/        $allRow = $currentSheet-&gt;getHighestRow();        echo $allColumn . " -- " . $allRow . "<br>";        //循环读取每个单元格的内容。注意行从1开始,列从A开始        for ($rowIndex = 2; $rowIndex getCell($addr)-&gt;getvalue();                array_push($data, $cell);            }            var_dump($data);            $this-&gt;updataForExcel($data);        }        unlink($filePath);    }    /**     * 根据Execl数据更新数据库     * @param array $data     * $data[0] --&gt; name    客户姓名     * $data[1] --&gt; sex     性别     * $data[2] --&gt; cellphone   联系方式     * $data[3] --&gt; knowchannel 认知途径     * $data[4] --&gt; intent_size 需求面积     * $data[5] --&gt; intent_huxing   需求户型     * $data[6] --&gt; prices_reflect  价格反映     * $data[7] --&gt; intent_desc     置业目的     * $data[8] --&gt; focus_desc      关注点     * $data[9] --&gt; nofocus_desc    不认可点     * $data[10] --&gt; buytime        置业次数     * $data[11] --&gt; locdesc        居住区域     * $data[12] --&gt; intent_level   意向级别     * $data[13] --&gt; note           备注     */    function updataForExcel($data = array())    {        if (count($data) == 0) {            return;        }        $cellphone = $data[2];        if (isset($cellphone)) {            $info = $this-&gt;useinfo_tag_db-&gt;get_one("cellphone = $cellphone");            $settime = time();            if (null != $info) {//原数据存在,修改                $sql = "update useinfo_tag set name='$data[0]',sex='$data[1]',knowchannel='$data[3]',";                $sql .= "intent_size='$data[4]',intent_huxing='$data[5]',prices_reflect='$data[6]',";                $sql .= "intent_desc='$data[7]',focus_desc='$data[8]',nofocus_desc='$data[9]',";                $sql .= "buytime='$data[10]',locdesc='$data[11]',intent_level='$data[12]',";                $sql .= "note='$data[13]',settime=$settime";                $sql .= " where cellphone = '$cellphone'";                $result = $this-&gt;useinfo_tag_db-&gt;query($sql);                if ($result) {                    echo "修改成功";                } else {                    echo "修改失败";                }            } else {//没有当前数据,插入新数据                $sql = "insert into useinfo_tag(name,sex,cellphone,knowchannel,intent_size,intent_huxing,";                $sql .= "prices_reflect,intent_desc,focus_desc,nofocus_desc,buytime,locdesc,intent_level,note,settime)";                $sql .= " values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]',";                $sql .= "'$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]',$settime)";                $result = $this-&gt;useinfo_tag_db-&gt;query($sql);                if ($result) {                    echo "插入成功";                } else {                    echo "插入失败";                }            }            echo "<br>";        }    }

html部分代码:

<span><script><span>language=</script></span><span>"javascript" </span><span>type=</span><span>"text/javascript" </span><span>src=</span><span>"</span><span><?php echo </span><span><em>JS_PATH </em></span><span>?&gt;</span><span>jquery.form.js"</span><span>&gt;</span></span>

<span><form><span>id=</span><span>"form_file" </span><span>action=</span><span>"?m=kfqapp&amp;c=useinfo_tag&amp;a=getExeclData" </span><span>method=</span><span>"post"</span><span>enctype=</span><span>"multipart/form-data"</span><span>&gt;</span><span>    <label><span>for=</span><span>"file"</span><span>&gt;</span><span>导入</span>Excel<span>表:</span><span></span></label></span><span>    <input><span>type=</span><span>"file" </span><span>name=</span><span>"file" </span><span>id=</span><span>"file"</span><span>/&gt;</span><span>    <input><span>type=</span><span>"button" </span><span>id=</span><span>"upfileSubmit" </span><span>name=</span><span>"upfileSubmit" </span><span>value=</span><span>"</span><span>提交</span><span>"</span><span>/&gt;</span><span></span></span></span></form></span>

<span>$</span>(<span>"#upfileSubmit"</span>).<span>click</span>(<span>function </span>() {    <span>var </span>options = {        <span>beforeSend</span>: <span>function </span>() {            <span>//console.log("</span><span>开始</span><span>");</span><span>$</span>(<span>'#container'</span>).<span>css</span>(<span>"display"</span><span>, </span><span>"block"</span>)<span>;</span>}<span>,</span><span>success</span>: <span>function </span>(data) {            <span>//console.log("</span><span>结束</span><span>");</span><span>$</span>(<span>'#container'</span>).<span>css</span>(<span>"display"</span><span>, </span><span>"none"</span>)<span>;</span><span>window</span>.<span>location</span>.<span>reload</span>()<span>;</span>}    }    <span>$</span>(<span>"#form_file"</span>).<span>ajaxSubmit</span>(options)<span>;</span>})<span>;</span>

以上就介绍了剑灵捏人数据怎么导入 php导入execl表到数据库,包括了剑灵捏人数据怎么导入方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

剑灵捏人数据怎么导入