劍靈捏人資料怎麼匯入 PHP匯入Execl表到資料庫

來源:互聯網
上載者:User
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"] < 204800 && in_array($extension, $allowedExts)) {   // 小於 200 kb            if ($_FILES["file"]["error"] > 0) {                echo "錯誤:: " . $_FILES["file"]["error"] . "
"; 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->uploadFileforExcel(); if ($filePath == null || $filePath == '') { return; } //建立reader對象 $PHPReader = new PHPExcel_Reader_Excel2007(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new PHPExcel_Reader_Excel5(); if (!$PHPReader->canRead($filePath)) { echo 'no Excel'; return; } } //建立excel對象,此時你即可以通過excel對象讀取檔案,也可以通過它寫入檔案 $PHPExcel = $PHPReader->load($filePath); /**讀取excel檔案中的第一個工作表*/ $currentSheet = $PHPExcel->getSheet(0); /**取得最大的列號*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); echo $allColumn . " -- " . $allRow . "
"; //迴圈讀取每個儲存格的內容。注意行從1開始,列從A開始 for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) { $data = array(); for ($colIndex = 'A'; $colIndex <= 'N'; $colIndex++) { $addr = $colIndex . $rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); array_push($data, $cell); } var_dump($data); $this->updataForExcel($data); } unlink($filePath); } /** * 根據Execl資料更新資料庫 * @param array $data * $data[0] --> name 客戶姓名 * $data[1] --> sex 性別 * $data[2] --> cellphone 連絡方式 * $data[3] --> knowchannel 認知途徑 * $data[4] --> intent_size 需求面積 * $data[5] --> intent_huxing 需求戶型 * $data[6] --> prices_reflect 價格反映 * $data[7] --> intent_desc 置業目的 * $data[8] --> focus_desc 關注點 * $data[9] --> nofocus_desc 不認可點 * $data[10] --> buytime 置業次數 * $data[11] --> locdesc 居住地區 * $data[12] --> intent_level 意向層級 * $data[13] --> note 備忘 */ function updataForExcel($data = array()) { if (count($data) == 0) { return; } $cellphone = $data[2]; if (isset($cellphone)) { $info = $this->useinfo_tag_db->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->useinfo_tag_db->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->useinfo_tag_db->query($sql); if ($result) { echo "插入成功"; } else { echo "插入失敗"; } } echo "
"; } }

html部分代碼:



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

以上就介紹了劍靈捏人資料怎麼匯入 PHP匯入Execl表到資料庫,包括了劍靈捏人資料怎麼匯入方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.