php上傳xls檔案匯入到mysql資料表,
申明下面的文章屬於轉載,現在大家新下載的版本跟下面列子這個不一樣!我看了很多網上的列子,最後我還是讀了那個英文的手冊,上面說的很清楚!大家英語不好可以下載有道!其實重點是要包含下面兩行:
require_once 'excellib/PHPExcel.php';
require_once 'excellib/PHPExcel/IOFactory.php';
你們各自的替換各自存放目錄,我這裡用的是excellib目錄。就是要把PHPExcel.php',PHPExcel/IOFactory.php包含進來就可以,其他的跟後面這個例子差不多。
本功能實際上是通過一個國外php對xls檔案讀取的類實現的,網上的資料多是excel檔案另存新檔csv檔案,然後從csv檔案匯入。
PHP-ExcelReader,: http://sourceforge.net/projects/phpexcelreader 下載解壓後,
目錄結構
首先我們需要一個上傳頁面:
代碼如下:
<!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=gb2312" />
<title>匯入測試</title>
</head>
<body>
<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="../skins/top_bg.gif"><label> <strong><a href="#">小學數學題目資料匯入</a></strong></label></td>
</tr>
<tr>
<td width="18%" height="50"> 選擇你要匯入的資料表</td>
<td width="82%"><label>
<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>execel</strong>檔案格式{.<span class="STYLE2">xls</span>}為副檔名.</td>
</tr>
<tr>
<td colspan="2"> 2、execel檔案匯入資料順序必須如:序號 | 題目 </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr></form>
</table>
</body>
</html>
資料庫連接字碼頁:
<?php
$host="localhost";
$user="root";
$password="123456";
$database="project";
$connect=@mysql_connect("$host","$user","$password");
if(!$connect)
{
echo "database connect wrong";
exit;
}
$db=mysql_select_db("$database",$connect);
$sql=mysql_query("SET NAMES 'gb2312'");
?>
讀取插入的頁面
代碼如下:
<?php
error_reporting(E_ALL ^ E_NOTICE);
if($_POST){
$Import_TmpFile = $_FILES['file']['tmp_name'];
require_once 'conn.php';
mysql_select_db('test_xls'); //選擇資料庫
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){
$Isql = "Select id from xls where id='".$tmp[1]."'";
$sql = "Insert into xls (id,tm) value(";
$sql.="'".$tmp[1]."','".$tmp[2]."')";
if(! mysql_num_rows(mysql_query($Isql) )){
if( mysql_query($sql) ){
$count++;
}
}
$total++;
}
echo "<script>alert('共有".$total."條資料,匯入".$count."條資料成功');</script>";
}
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;
}
?>
資料庫testz_xls表
測試環境 windows xp
phpnow 1.4
地址:http://localhost/test/up.php
測試圖:
本文出自 “成長流水賬----開源世界,分享知識的快樂!” 部落格,請務必保留此出處http://jason2016.blog.51cto.com/892969/289411本文出自 51CTO.COM技術部落格