2010-07-02 126 views

回答

3

CSV
如果你可以先在Excel文件轉換爲CSV,你可以使用mysqlimport導入CSV。這可能是將數據導入MySQL的最快捷方法。

您可以使用LOAD DATA INFILE從PHP進行此操作。這是一個示例SQL語句導入data.csv

LOAD DATA INFILE 'data.csv' INTO TABLE phonenumber_list 
FIELDS TERMINATED BY ',' ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n' 
IGNORE 1 LINES; 

Excel中
如果您不能使用CSV,並需要使用原始Excel文件的工作,你需要一個PHP庫,它能夠讀取Excel文件。

有幾個可用的,但我不知道他們是多麼可靠或維護多麼好:

Pear: Spreadsheet_Excel_Writer

PHPExcel

PHP-ExcelReader

你也可能想看看在使用Excel API的替代方法中,但您需要安裝Excel才能執行此操作。有關於這裏一點信息:

http://www.sydphp.org/presentations/010606-excel.html

如果使用這種方法,你需要編寫一些代碼,讀取並分析Excel文件,並將其發送到MySQL一行接一行。這可能會比批量CSV導入慢得多。

+0

mysqlimport和LOAD DATA INFILE,只能使用CSV或將其與EXCEl文件一起使用... – 2010-07-02 06:52:48

+0

對不起,我沒有說清楚。 'LOAD DATA INFILE'只能用於CSV文件。我已經更新了我的答案。 – Mike 2010-07-02 07:09:30

+0

嘿!謝謝你... @麥克... – 2010-07-02 07:29:57

3

首先,您必須將Excel文件轉換爲CSV格式(逗號分隔)文件。

然後使用下面的PHP腳本插入它在MySQL:

<?php 
error_reporting(E_ALL & ~E_NOTICE); 
$connect = mysql_connect("localhost","root",""); 
@mysql_select_db("dbname"); 

[email protected]$_FILES[csv][name]; 
$ext=end(explode('.',$inputFile)); 

if($ext=='csv') 
{ 
    [email protected]$_FILES[csv][tmp_name]; 
    $f = @fopen($tmpFile, 'r'); 
    $contents = @fread($f, 10000); 
    fclose($f); 
    $fileLines = explode("\r\n", $contents); // explode to make sure we are only using the first line. 
    $fieldList_header = explode(',', $fileLines[0]); 
    $csv_header=array("ID","Name","Address","Email-Id","Contact-No"); // Excel file heading 

    $i=0; 
    //index keys for each column 
    $ID_key=null; 
    $Name_key=null; 
    $Address_key=null; 
    $Email_ID_key=null; 
    $Contact_No_key=null; 

    foreach($csv_header as $csv) 
    { 
     //searching index for each value 
     if(in_array($csv,$fieldList_header)) 
     { 
      $key=array_search($csv,$fieldList_header); 

      if($i==0) 
       $ID_key=$key; 
      if($i==1) 
       $Name_key=$key; 
      if($i==2) 
       $address_key=$key; 
      if($i==3) 
       $Email_ID_key=$key; 
      if($i==4) 
       $Contact_No_key=$key; 
     } 
     else 
     { 
      echo "Failed to search : ".$csv." blank space will be inserted.<br>"; 
     } 
     $i++; 
    } 
    echo "<table border=1px>"; 
    echo "<tr>"; 
     echo "<td>"; 
      echo "Id"; 
     echo "</td>"; 
     echo "<td>"; 
      echo "Name"; 
     echo "</td>"; 
     echo "<td>"; 
      echo "Address"; 
     echo "</td>"; 
     echo "<td>"; 
      echo "Email ID"; 
     echo "</td>"; 
     echo "<td>"; 
      echo "Contact No"; 
     echo "</td>"; 
    echo "</tr>"; 
    $j=1; 
    for($i=2;$i<count($fileLines)-1;$i++) 
    { 

     $fieldList_other = explode(',', $fileLines[$i]); 
     echo "<tr>"; 
      echo "<td>"; 
       echo $fieldList_other[$ID_key]; 
      echo "</td>"; 
      echo "<td>"; 
       echo $fieldList_other[$Name_key]; 
      echo "</td>"; 
      echo "<td>"; 
       echo $fieldList_other[$Address_key]; 
      echo "</td>"; 
      echo "<td>"; 
       echo $fieldList_other[$Email_ID_key]; 
      echo "</td>"; 
      echo "<td>"; 
       echo $fieldList_other[$Contact_No_key]; 
      echo "</td>"; 
     echo "</tr>"; 

     $query="insert into Contact (EmployeeName,DateOfJoining,SalaryPerDay,Address,Designation,BalanceLeaves,ContactNumber) values('$fieldList_other[$ID_key]','$fieldList_other[$Name_key]','$fieldList_other[$Address_key]','$fieldList_other[$Email_ID_key]','$fieldList_other[$Contact_No_key]')"; 

     $result=mysql_query($query); 
     if(!$result) 
      echo "Record $j failed.<br>"; 
     else 
      echo "Record $j inserted in database."; 
     $j++; 
    } 
    echo "</table>";  
} 
?> 




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
     <title>Import a CSV File with PHP & MySQL</title> 
     <script> 
      <!-- 
      function noempty() 
      { 
      var y=document.forms["form1"]["csv"].value; 
      if (y==null || y=="") 
       { 
       alert("Please enter the required field"); 
       return false; 
       } 

      } 
      --> 
     </script> 
    </head> 
<body> 
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
<center> 
</br></br> 
    Choose your file: <br /> 
    <input name="csv" type="file" id="csv" /> 
    <input type="submit" name="Submit" value="Submit" onclick="return noempty()"/> 
    </center> 
</form> 
</body> 

</html> 

這個腳本會幫助你。因爲無論Excel文件列的順序如何,相應的數據只能在MySQL中輸入。

相關問題