PHP讀取EXCEL文件寫入數據庫

PHP讀取ECEL文件寫入數據庫功能的實現使用到了PHPExcel類庫。完整代碼如下:

<?php  

   $uploadfile="../upload_files/".basename($_FILES['userfile']['name']);

   $message="";

   if(@move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)){

     

      //文件上傳服務器成功,將文件導入數據庫中

      require_once '../libraries/PHPExcel.php';

      $filePath=$uploadfile;

      //excel表格中讀取信息     

      $PHPExcel= new PHPExcel();

     

      /**默認用excel2007讀取excel,若格式不對,則用之前的版本進行讀取*/ 

      $PHPReader= new PHPExcel_Reader_Excel2007();  

      if(!$PHPReader->canRead($filePath))

      { 

          $PHPReader = new PHPExcel_Reader_Excel5();

          if(!$PHPReader->canRead($filePath))

          {

              $message='文件不存在!';

              return

          }

      }   

      $PHPExcel= $PHPReader->load($filePath);

     

      /**讀取excel文件中的第一個工作表*/

     

      $currentSheet= $PHPExcel->getSheet(0);

     

      /**取得最大的列號*/

      $allColumn= $currentSheet->getHighestColumn();

     

      /**取得一共有多少行*/ 

      $allRow= $currentSheet->getHighestRow();

     

      //連接數據庫

      include_once '../libraries/db.php';

      $db=new Db();

      $conn= $db->mssqlConnetion();

      if($conn ===false)

      {

        die( print_r( sqlsrv_errors(),true));

        return NULL;

      }else{//連接成功

                

        /**從第二行開始輸出,因爲excel表中第一行爲列名*/

        for($currentRow =2;$currentRow <= $allRow;$currentRow++)

        {

        $info_id= $PHPExcel->getActiveSheet()->getCell("A".$currentRow)->getValue();//獲取A列的值

         $info_title= $PHPExcel->getActiveSheet()->getCell("B".$currentRow)->getValue();//獲取B列的值

         $info_author= $PHPExcel->getActiveSheet()->getCell("C".$currentRow)->getValue();//獲取C列的值

         $info_origin= $PHPExcel->getActiveSheet()->getCell("D".$currentRow)->getValue();//獲取D列的值

         $info_theme= $PHPExcel->getActiveSheet()->getCell("E".$currentRow)->getValue();//獲取E列的值

         $info_keyword= $PHPExcel->getActiveSheet()->getCell("F".$currentRow)->getValue();//獲取F列的值

         $info_pubtime= $PHPExcel->getActiveSheet()->getCell("G".$currentRow)->getValue();//獲取G列的值

         $info_abstract= $PHPExcel->getActiveSheet()->getCell("F".$currentRow)->getValue();//獲取H列的值

         $info_format= $PHPExcel->getActiveSheet()->getCell("I".$currentRow)->getValue();//獲取I列的值

         $info_isfull= $PHPExcel->getActiveSheet()->getCell("G".$currentRow)->getValue();//獲取J列的值

         $info_isdown= $PHPExcel->getActiveSheet()->getCell("K".$currentRow)->getValue();//獲取K列的值

         $info_trade= $PHPExcel->getActiveSheet()->getCell("L".$currentRow)->getValue();//獲取L列的值

         $info_category= $PHPExcel->getActiveSheet()->getCell("M".$currentRow)->getValue();//獲取M列的值

         $info_path= $PHPExcel->getActiveSheet()->getCell("N".$currentRow)->getValue();//獲取N列的值

         $info_remark= $PHPExcel->getActiveSheet()->getCell("O".$currentRow)->getValue();//獲取O列的值

     

         $info_title=iconv('utf-8','GB2312//IGNORE', $info_title);//消除亂碼

         $info_author=iconv('utf-8','GB2312//IGNORE', $info_author);//消除亂碼

         $info_origin=iconv('utf-8','GB2312//IGNORE', $info_origin);//消除亂碼

         $info_theme=iconv('utf-8','GB2312//IGNORE', $info_theme);//消除亂碼

         $info_keyword=iconv('utf-8','GB2312//IGNORE', $info_keyword);//消除亂碼

         $info_pubtime=iconv('utf-8','GB2312//IGNORE', $info_pubtime);//消除亂碼

         $info_abstract=iconv('utf-8','GB2312//IGNORE',$info_abstract);//消除亂碼

         $info_format=iconv('utf-8','GB2312//IGNORE', $info_format);//消除亂碼

         $info_isfull=iconv('utf-8','GB2312//IGNORE', $info_isfull);//消除亂碼

         $info_isdown=iconv('utf-8','GB2312//IGNORE', $info_isdown);//消除亂碼

         $info_trade=iconv('utf-8','GB2312//IGNORE', $info_trade);//消除亂碼

         $info_category=iconv('utf-8','GB2312//IGNORE',$info_category);//消除亂碼

         $info_path=iconv('utf-8','GB2312//IGNORE', $info_path);//消除亂碼

         $info_remark=iconv('utf-8','GB2312//IGNORE', $info_remark);//消除亂碼  

         //數據庫操作

        $sql= "INSERT INTO bio_Information(Info_ID,Info_Title,Info_Author,Info_Origin,Info_Theme,Info_Keyword,Info_PubTime,Info_Abstract,Info_Format,Info_IsFull,Info_IsDown,Info_Trade,Info_Category,Info_Path,Info_Remark)                 VALUES('".$info_id."','".$info_title."','".$info_author."','".$info_origin."','".$info_theme."','".$info_keyword."','".$info_pubtime."','".$info_abstract."','".$info_format."','".$info_isfull."','".$info_isdown."','".$info_trade."','".$info_category."','".$info_path."','".$info_remark."')";

        $params= array();

        $options=  array("Scrollable"=> SQLSRV_CURSOR_KEYSET );

        $Stmt= sqlsrv_query( $conn, $sql , $params, $options );     

        if ($Stmt===false)

           {

           die( print_r( sqlsrv_errors(),true));

           return NULL

           }  

        }

      }       

      //循環結束,判斷全部數據是否插入

      if($currentRow >$allRow){

      //echo '文件插入成功!';

      $message='文件上傳成功!';

      }else{

      //echo '文件插入失敗!';    

      $message='文件上傳失敗!';  

      }      

   }else{

      $message='文件上傳受阻!';

   }

   print "{success:true,msg:'".$message."'}";

  

?>

以上代碼,只實現了上傳入庫的功能,對於文件大小、重名、上傳失敗處理等細節未能實現,需再次加工。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章