申明下面的文章屬于轉(zhuǎn)載,現(xiàn)在大家新下載的版本跟下面列子這個(gè)不1樣!我看了很多網(wǎng)上的列子,最后我還是讀了那個(gè)英文的手冊(cè),上面說(shuō)的很清楚!大家英語(yǔ)不好可以下載有道!其實(shí)重點(diǎn)是要包括下面兩行:
require_once 'excellib/PHPExcel.php';
require_once 'excellib/PHPExcel/IOFactory.php';
你們各自的替換各自寄存目錄,我這里用的是excellib目錄。就是要把PHPExcel.php',PHPExcel/IOFactory.php包括進(jìn)來(lái)就能夠,其他的跟后面這個(gè)例子差不多。
本功能實(shí)際上是通過(guò)1個(gè)國(guó)外php對(duì)xls文件讀取的類實(shí)現(xiàn)的,網(wǎng)上的資料多是excel文件另存為csv文件,然后從csv文件導(dǎo)入。
PHP-ExcelReader,下載地址: http://sourceforge.net/projects/phpexcelreader 下載解壓后,
目錄結(jié)構(gòu)
首先我們需要1個(gè)上傳頁(yè)面:
代碼以下:
<!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>導(dǎo)入測(cè)試</title>
</head>
<body>
<script>
function imp
var f_content = form1.file.value;
var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length)
fileext=fileext.toLowerCase()
if (fileext!='.xls')
{
alert("對(duì)不起,導(dǎo)入數(shù)據(jù)格式必須是xls格式文件哦,請(qǐng)您調(diào)劑格式后重新上傳,謝謝 !");
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-da
<tr >
<td height="28" colspan="2" background="../skins/top_bg.gif"><label> <strong><a href="#">小學(xué)數(shù)學(xué)題目數(shù)據(jù)導(dǎo)入</a></strong></label></td>
</tr>
<tr>
<td width="18%" height="50"> 選擇你要導(dǎo)入的數(shù)據(jù)表</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="導(dǎo)入數(shù)據(jù)" on
</label>
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#DDF0FF"> [<span class="STYLE1">注</span>]數(shù)據(jù)導(dǎo)入格式說(shuō)明:</td>
</tr>
<tr>
<td colspan="2"> 1、其它.導(dǎo)入數(shù)據(jù)表文件必須是<strong>execel</strong>文件格式{.<span class="STYLE2">xls</span>}為擴(kuò)大名.</td>
</tr>
<tr>
<td colspan="2"> 2、execel文件導(dǎo)入數(shù)據(jù)順序必須如:序號(hào) | 題目 </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr></form>
</table>
</body>
</html>
數(shù)據(jù)庫(kù)連接代碼頁(yè):
<?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'");
?>
讀取插入的頁(yè)面
代碼以下:
<?php
error_reporting(E_ALL ^ E_NOTICE);
if($_POST){
$Imp
require_on
mysql_select_db('test_xls'); //選擇數(shù)據(jù)庫(kù)
require_on
$da
$da
$da
$array =array();
for ($i = 1; $i <= $da
for ($j = 1; $j <= $da
$array[$i][$j] = $da
}
}
sava_da
}
function sava_da
$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."條數(shù)據(jù),導(dǎo)入".$count."條數(shù)據(jù)成功');</script>";
}
function TtoD($text){
$jd1900 = GregorianToJD(1, 1, 1900)⑵;
$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;
}
?>
數(shù)據(jù)庫(kù)testz_xls表
測(cè)試環(huán)境 windows xp
phpnow 1.4
地址:http://localhost/test/up.php
測(cè)試圖:
本文出自 “成長(zhǎng)流水賬----開(kāi)源世界,分享知識(shí)的快樂(lè)!” 博客,請(qǐng)務(wù)必保存此出處http://jason2016.blog.51cto.com/892969/289411本文出自 51CTO.COM技術(shù)博客