上传 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
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();
    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()))."‘".‘,‘.
                    //strtotime(date(‘Y-m-d H:i:s‘)).",".
                    "‘".strtoupper(trim($objPHPExcel->getActiveSheet()->getCell("Q".$rowIndex)->getValue()))."‘".‘)‘;
            }
        }//此处A-Q表示  表格的列到哪个字母 这里就写到哪个字母 (根据自身的需求)
        mysql_connect("localhost","root","root");
        mysql_select_db("count");
        mysql_query("SET AUTOCOMMIT=0");//设置为不自动提交,因为MYSQL默认立即执行
        mysql_query("BEGIN");//开始事务定义
        mysql_query("set names utf8");
        foreach($new_arr as $vv){
            $sql=mysql_query("replace into wangyou (addtime,sp_code,sp_name,yw_code,yw_name,month_payuser,
            month_xzuser,day_payuser,day_pay,day_paycs,month_pay,qsst_payuser,content_class,day_paybl,day_syuser,month_syuser,qsst_syuser) values $vv");
            if(!$sql){
                mysql_query("ROOLBACK");//判断执行失败回滚
            }
        }
        mysql_query("COMMIT");//执行事务
    }
    $count=mysql_num_rows(mysql_query("select * from wangyou"));
    $chongfu=$allRow-$count-1;
    echo "网游管控成功入库".$count."条".","."重复".$chongfu."条";
?>



这里的去重    用到了mysql里的唯一索引 unique   当然用到unique时相应的sql就变成replace into


本文出自 “php开发” 博客,转载请与作者联系!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。