知識社群ePortfolio登入
位置: 王敦正 > 程式設計 > PHP
使用PHP將Excel匯入mysql
by 王敦正 2016-05-12 12:11:19, 回應(0), 人氣(5238)
如何將excel透過PHP將Excel直接匯入資料庫,經過查詢 大多是使用先將Excel轉為CSV檔,再匯入mydql,這樣做實在是件累人的事,當然 科技始終來自於惰性!後來終於找到一個可以直接將Excel直接透過PHP匯入mysql的套件.
要使用這個套件 要先到這下載 http://sourceforge.net/projects/phpexcelreader  PHP-ExcelReader,這是一個開放原始碼的套件,主要是要用來解析Excel文件,下載好後直接解壓縮,主要是會用到Excel資料夾裡面的readyer.php 與 oleready.php 二個檔案。原始下載下來的olread.php是olread.inc,請將此檔案直接更改檔名為oleready.php。
在Excel/reader.php檔案裡的第一行改成正確的 require_once ' oleread.php ' ;
Excel透過PHP匯入mysql的table, 程序上會使用到三個檔案作為匯入Excel的全部程序。
檔案1.upphp  檔案2.insert.php  檔案3. conn.php
首先 在up.php 置入以下程式碼:
<script> 
function import_check(){
var f_content = form1.file.value;
var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length) 
fileext=fileext.toLowerCase() 
if (fileext!='.xls') 
{
alert("對不起,導入資料格式必須是xls格式文件哦,請您調整格式後重新上傳,謝謝!");
return false;
} } 
</script>
<table width="98%" border="0" align="center" style="margin-top:20px; border:1px solid #9abcde;"> 
<form id="form1" name="form1" enctype="multipart/form-data" method="POST" action="insert.php">
<tr > 
<td height="28" colspan="2" background=" "><label> <strong><a href="#">檔案上傳
</a></strong></label></td> 
</tr> 
<tr> 
<td width="18%" height="50"> 選擇你要導入的Excel資料表</td>
<input name="file" type="file" id="file" size="50" />
</label>
<label> 
<input name="button" type="submit" class="nnt_submit" id="button" value="導入資料" onclick="import_check();"/>  
</label> 
</td> 
</tr> 
<tr> 
<td colspan="2" bgcolor="#DDF0FF"> [<span class="STYLE1">注</span>]資料表導入格式說明:</td> 
</tr>
<tr> 
<td colspan="2"> 1、其它.導入資料表文件必須是<strong>excel</strong>文件格式{.<span class="STYLE2">xls</span>}
為副檔名. </td> 
</tr> 
<tr>
<td colspan="2">&nbsp;</td> 
<tr> <tr> 
<td colspan="2"> </td>
</tr></form> 
</table>
-------------------------------------------------------------------
<body>程式碼要放在這裡面  
接著將下段程式法 複製到insert.php
//error_reporting(E_ALL ^​​ E_NOTICE);
if($_POST){ 
$Import_TmpFile = $_FILES['file']['tmp_name']; 
require_once ('Connections/speedforsunshen.php'); 
mysql_select_db('sunshun'); //選擇資料庫
require_once('Excel/reader.php'); 
$data = new Spreadsheet_Excel_Reader(); 
$data->setOutputEncoding('UTF-8');
$data->read($Import_TmpFile);
$array =array();
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { 
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { 
$array[$i][$j] = $data->sheets[0]['cells'][$i][$j]; 
}
}
sava_data($array); 
}  
function sava_data($array){
$count =0; 
$total =0; 
foreach( $array as $tmp){ 
$qq =  date("Y:m:d H:i:s"); 
$Isql = "Select * from exceltest where id='".$tmp[1]."'"; 
$sql = "INSERT INTO exceltest(id,a2,a3,a4,a5,a6,a7,a8,a9,a10,importdate) VALUES("; //資料庫的table欄位要記得更改.
$sql.=" '$tmp[1]','$tmp[2]','$tmp[3]','$tmp[4]','$tmp[5]','$tmp[6]','$tmp[7]','$tmp[8]','$tmp[9]','$tmp[10]','$qq')";
$result = mysql_query($sql) or die("無法送出" . mysql_error( ));
echo $sql."
";
if(! mysql_num_rows(mysql_query($Isql) )){ //取得返回列的數目  送出一個 query 字符串。
if( mysql_query($sql) ){ 
}  
}  
$count++; 
$total++; 
}  
echo " "; 
}
function TtoD($text){ 
$jd1900 = GregorianToJD(1, 1, 1900)-2; 
$myJd = $text+$jd1900; 
$myDate = JDToGregorian($myJd);
$myDate = explode('/',$myDate); 
$myDateStr = str_pad($myDate[2],4,'0', STR_PAD_LEFT)."-".str_pad($myDate[0],2,'0', STR_PAD_LEFT)."-".str_pad($myDate 
[1],2,'0', STR_PAD_LEFT); 
return $myDateStr; 

?>
一樣 程式碼也是要貼在body之間
最後將以下程式碼貼到conn.php
//$host="serverip";      //請修改 登入資料庫的帳號密碼及server主機位置(ip)
//$user="yourusername"; 
//$password="yourpwd"; 
$database="yourDB"; 
$connect=mysql_connect("$host","$user","$password");
if(!$connect) 

echo "database connect wrong";
exit; 

$db=mysql_select_db("$database",$connect); 
$sql=mysql_query("SET NAMES 'utf-8'"); 
?> 
-----------------------------------------------------
程式碼也是要貼在body之間
這樣就可以直接執行將Excel匯入資料庫囉!!