
如圖,在Excel導(dǎo)入插件中,當(dāng)上傳Excel表時(shí),如何判斷已有表中,指定某字段的重復(fù)數(shù)據(jù),最終該數(shù)據(jù)只導(dǎo)入新數(shù)據(jù),并提示重復(fù)數(shù)據(jù)數(shù)量與明細(xì)?附相關(guān)代碼。
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('上傳文件移動(dòng)失敗');
}
// 提交參數(shù)
$ids = $this->input->post('ids');
$post = $this->input->post('data');
/** Include PHPExcel */
require APPPATH.'Classes/Init.php';
//建立reader對(duì)象
$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對(duì)象
$currentSheet = $PHPExcel->getSheet($sheet); //**讀取excel文件中的指定工作表*/
$allColumn = $currentSheet->getHighestColumn(); //**取得最大的列號(hào)*/
$allRow = $currentSheet->getHighestRow(); //**取得一共有多少行*/
$data = array();
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ //循環(huán)讀取每個(gè)單元格的內(nèi)容。注意行從1開(kāi)始,列從A開(kāi)始
for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText){ //富文本轉(zhuǎn)換字符串
$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文件解析數(shù)據(jù)失敗');
}
$count = 0;
// 數(shù)據(jù)處理
foreach ($data as $i => $t) {
// 驗(yàn)證行數(shù)
if ($post['ks'] && $i<$post['ks']) {
continue;
}
// 驗(yàn)證不能為空
$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('共導(dǎo)入'.$count.'個(gè)', '', 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');
}
感謝分享
回復(fù)@黃卿 哥們,我提的是問(wèn)題,不是分享呢,看來(lái)我得在標(biāo)題上加個(gè)問(wèn)號(hào)。。。
不是一兩句代碼能描述清楚,私聊給你做好
回復(fù)@誠(chéng)接二開(kāi)/定制/使用解答 已經(jīng)QQ聯(lián)絡(luò)你,但是還未得到答復(fù),請(qǐng)QQ私聊。
這個(gè)插件早就不能用了,官方都不更新了
回復(fù)@黃卿 是的,但是這個(gè)還是比較實(shí)用的一個(gè)插件,也不清楚為什么官方就不上架與更新。
這個(gè)插件不適用,沒(méi)必要用,表太廣泛,沒(méi)有定向表數(shù)據(jù)導(dǎo)入,不適用
回復(fù)@小青年 打算自己做一個(gè)定向表數(shù)據(jù)導(dǎo)入,已經(jīng)成功一半了,現(xiàn)在需要處理重復(fù)數(shù)據(jù)問(wèn)題。你還有更好的解決辦法嗎?麻煩一起分享一下。
我正在找這個(gè)Excel插件,能給我一份嗎
回復(fù)@秦 可以聯(lián)系我QQ:六一78三個(gè)2一個(gè)五,一起研究吧。
自己嘗試已經(jīng)將問(wèn)題解決,并且將官方代碼二次開(kāi)發(fā)了一部分,現(xiàn)在作為通用型欄目導(dǎo)入導(dǎo)出,比較好用了。