標籤:phpexcel類 上傳 入庫後 去重
upload.html 頁面<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" id="file" name="file" > <input type="button" onclick="file.click()" value="選擇附件"> <input type="submit" value="提交" /> </form> upload.php<?php//set_time_limit(300);設定逾時時間require_once "../upload/PHPExcel_1.8.0_doc/Classes/PHPExcel/IOFactory.php";$object =new PHPExcel();$filePath =$_FILES["file"]["tmp_name"];$fileType = PHPExcel_IOFactory::identify($filePath); //檔案名稱自動判斷檔案類型$objReader = PHPExcel_IOFactory::createReader($fileType);$objPHPExcel = $objReader->load($filePath);$currentSheet = $objPHPExcel->getSheet(0); //第一個活頁簿$allRow = $currentSheet->getHighestRow(); //行數 /**取得最大的列號*/ $allColumn = $currentSheet->getHighestColumn();//迴圈讀取每個儲存格的內容。注意行從1開始,列從A開始 /*for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ for($colIndex=‘A‘;$colIndex<=$allColumn;$colIndex++){ $addr = $colIndex.$rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); if($cell instanceof PHPExcel_RichText) //文本轉換字串 $cell = $cell->__toString(); $sql="insert into tongji (addtime,provice,partner_code,partner,yw_code,yw_name,day_total, day_dstotal,day_hftotal,day_xzuser,day_xzcs,day_freeuser,day_freecs,day_payuser,day_paycs ,day_loginuser,day_logincs,day_falshuser,day_flashcs,day_ARPU,day_newuser) values $cell" ; print_r($sql);die; $list=mysql_query($sql); print_r($list);die; } */ //$rowIndex=2;這個表示從表格的第二行開始 for($rowIndex=2;$rowIndex<=$allRow+1;$rowIndex++){ if($objPHPExcel->getActiveSheet()->getCell("A".$rowIndex)->getValue()){ if($objPHPExcel->getActiveSheet()->getCell("A".$rowIndex)->getValue()){ $hba=strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("A".$rowIndex)->getValue())); $hbb=strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("B".$rowIndex)->getValue())); $new_arr[]= ‘(\‘‘.$hba.‘\‘,‘. "‘".($hbb.‘‘)."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("C".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("D".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("E".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("F".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("G".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("H".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("I".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("J".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("K".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("L".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("M".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("N".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("O".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("P".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("Q".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("R".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("S".$rowIndex)->getValue()))."‘".‘,‘. "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("T".$rowIndex)->getValue()))."‘".‘,‘. //strtotime(date(‘Y-m-d H:i:s‘)).",". "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("U".$rowIndex)->getValue()))."‘".‘)‘; } } } mysql_connect("localhost","root","root"); $aa=mysql_select_db("count"); mysql_query("set names utf8"); //入庫 if(is_array($new_arr)){ $execl=implode(‘,‘,$new_arr); $sql=mysql_query("insert into tongji (addtime,province,partner_code,partner,yw_code,yw_name,day_total, day_dstotal,day_hftotal,day_xzuser,day_xzcs,day_freeuser,day_freecs,day_payuser,day_paycs ,day_loginuser,day_logincs,day_falshuser,day_flashcs,day_ARPU,day_newuser) values $execl"); $count=mysql_num_rows(mysql_query("select * from tongji")); $chongfu=$allRow-$count-1; echo "成功入庫".$count."條".","."重複".$chongfu."條"; }else{ echo 2; }
上傳 phpexcel 類 入庫後去重