ppypp伦理天堂,91手机在线视频,免费在线观看黄色毛片,夜夜穞天天穞狠狠穞AV美女按摩

聯系官方銷售客服

1835022288

028-61286886

投訴 已解決 Excel導入時,跳過重復數據,并提示重復數據數量與明細 11 0
frankxie 免費會員 2018-11-18 19:08:18 私信
POSCMS版本:3.3.1


image.png

如圖,在Excel導入插件中,當上傳Excel表時,如何判斷已有表中,指定某字段的重復數據,最終該數據只導入新數據,并提示重復數據數量與明細?附相關代碼。

public function import() {


    if (IS_POST) {
        $table = $this->input->post('table');
        if ($_FILES["file"]["error"] > 0) {
            $this->admin_msg('文件上傳失敗: '.$_FILES["file"]["error"]);
        } else {
            $ext = substr(strrchr($_FILES["file"]["name"], '.'), 1);
            $file = APPPATH.'cache/'.SYS_TIME.'.'.$ext;
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $file)) {
                if (!is_file($file)) {
                    $this->admin_msg('上傳文件移動失敗');
                }
                // 提交參數
                $ids = $this->input->post('ids');
                $post = $this->input->post('data');

                /** Include PHPExcel */
                require APPPATH.'Classes/Init.php';
                //建立reader對象
                $PHPReader = new PHPExcel_Reader_Excel2007();
                if(!$PHPReader->canRead($file)){
                    $PHPReader = new PHPExcel_Reader_Excel5();
                    if(!$PHPReader->canRead($file)){
                        $this->admin_msg('不是Excel文件');
                    }
                }

                $sheet = max(0, intval($post['id']) - 1);
                $PHPExcel = $PHPReader->load($file);        //建立excel對象
                $currentSheet = $PHPExcel->getSheet($sheet);        //**讀取excel文件中的指定工作表*/
                $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列號*/
                $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
                $data = array();
                for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){        //循環讀取每個單元格的內容。注意行從1開始,列從A開始
                    for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
                        $addr = $colIndex.$rowIndex;
                        $cell = $currentSheet->getCell($addr)->getValue();
                        if($cell instanceof PHPExcel_RichText){ //富文本轉換字符串
                            $cell = $cell->__toString();
                        } elseif (is_float($cell)) {
                            $time = PHPExcel_Shared_Date::ExcelToPHP($cell);
                            if ($time > 0) {
                                $cell = gmdate("Y-m-d H:i:s", $time);
                            }
                        }
                        $data[$rowIndex][$colIndex] = $cell;
                    }
                }

                if (!$data) {
                    @unlink($file);
                    $this->admin_msg('Excel文件解析數據失敗');
                }

                $count = 0;
                // 數據處理
                foreach ($data as $i => $t) {
                    // 驗證行數
                    if ($post['ks'] && $i<$post['ks']) {
                        continue;
                    }
                    // 驗證不能為空
                    $yz = 0;
                    $insert = array();
                    foreach ($ids as $id) {
                        if (is_null($t[$post[$id]['excel']])) {
                            $yz = 1;
                            continue;
                        }
                        $value = $t[$post[$id]['excel']];
                        if (isset($post[$id]['func']) && $post[$id]['func'] && function_exists($post[$id]['func'])) {
                            $value = call_user_func($post[$id]['func'], $value);
                        }
                        $insert[$id] = $value;
                    }
                    if ($yz) {
                        continue;
                    }


                    if ($insert) {
                        $this->db->insert($table, $insert);
                        $count ++;
                    }
                }
                @unlink($file);
                $this->admin_msg('共導入'.$count.'個', '', 1);
            } else {
                @unlink($file);
                $this->admin_msg('上傳失敗');
            }
        }

    }

    $this->template->assign(array(
        'table' => $_GET['table'],
        'abcd' => str_split(strtoupper('abcdefghijklmnopqrstuvwxyz'), 1),
    ));
    $this->template->display('import.html');

}
解決方案