php 匯入excel檔案mysql資料庫方法

來源:互聯網
上載者:User

先下載

下載phpexcel檔案,地址:phpexcel.codeplex.com/

在reader.php檔案中找到以下類似代碼(第一行既是),改成正確的oleread.php路徑即可:require_once 'oleread.php';

然後建立一個php檔案引入reader.php,


代碼如下:

 代碼如下 複製代碼

<?php
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('gbk');//此處設定編碼,一般都是gbk模式
 
$data->read('Book1.xls');//檔案路徑
 
error_reporting(E_ALL ^ E_NOTICE);
//這裡我就只迴圈輸出excel檔案的內容了,要入庫,只要把輸出的地方,寫一段mysql語句即可~
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
 for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
  echo """.$data->sheets[0]['cells'][$i][$j]."",";
 }
 echo "n";
}
?>


程式碼範例

 代碼如下 複製代碼

require_once 'phpexcel/Classes/PHPExcel.php';
require_once 'phpexcel/Classes/PHPExcel/IOFactory.php';
require_once 'phpexcel/Classes/PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($filename);   //$filename可以是上傳的檔案,或者是指定的檔案
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得總行數
$highestColumn = $sheet->getHighestColumn(); // 取得總列數
$k = 0;   

//迴圈讀取excel檔案,讀取一條,插入一條
   for($j=2;$j<=$highestRow;$j++)
   {

     $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//擷取A列的值
     $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//擷取B列的值
     $sql = "INSERT INTO table VALUES(".$a.",".$b.")";
     mysql_query($sql);

   }

代碼執行個體cvs匯入到資料庫

把csv匯入到資料庫。

 代碼如下 複製代碼

function   getmicrotime(){    
           
              list($usec,   $sec)   =   explode("   ",microtime());    
           
                    return   ((float)$usec   +   (float)$sec);  
            }  
   
  $time_start   =   getmicrotime();  
   
  include   ("connectdb.php");  
   
   
  function   insert_data   ($id,$summary,$description,$additional_information,$category)  
   
      {  
           
          $my_query1   =   "insert   into   mantis_bug_text_table   (id,description,additional_information)    
                 
          values   ('$id','$description','$additional_information')";  
         
          $first   =   mysql_query($my_query1);      
   
          $my_query2   =   "insert   into   mantis_bug_table   (id,project_id,summary,bug_text_id)   values   ('$id','$category','$summary','$id')";  
                 
          $second   =   mysql_query($my_query2);      
         
          return;  
      }  
   
  $fp   =   fopen("test.csv","r");  
   
  while($data   =   fgetcsv($fp,'1000',',')){  
   
              insert_data   ($data[0],$data[1],$data[2],$data[3],$data[4]);  
         
              echo   "<font   color   =   #ff0000   size   =   20>資料匯入成功!</font><br><br>";      
      }  
  fclose   ($fp);  
   
  $time_end   =   getmicrotime();  
   
  $time   =   $time_end   -   $time_start;  
   
  echo   "程式執行時間:".$time."秒"; 

更多詳細內容請查看:http://www.111cn.net/phper/php-database/excel-mysql.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.