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."'}";

  

?>

以上代码,只实现了上传入库的功能,对于文件大小、重名、上传失败处理等细节未能实现,需再次加工。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值