檔案上傳頁面
<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> 檔案上傳 </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"> [注]資料表導入格式說明:</td> </tr> <tr> <td colspan="2"> 1、其它.導入資料表文件必須是excel文件格式{.xls} 為副檔名. </td> </tr> <tr> <td colspan="2"> </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匯入資料庫囉!!
http://tw.myblog.yahoo.com/jw!OX2_ioqRCRtlZ0O7WBuAAPpHJhIu/article?mid=1978