这是我初次接触用PHP程序来读取Excel数据,刚开始可愁了,本来也没用过,就开始上网搜索这方面的资料,网上的说法真是众多风云啊!终于苍天不负有心人,让我找到了两种方法与大家分享一下。
第一种方法是先在ACCESS中导入*.xls文件,这样就可以从ACCESS数据库中读取数据,然后插入到mysql,sql server.....其它数据库,就搞定了。(我主要是导入到MYSQL中)
第二种方法是先把原来的*.xls文件另存为*.csv格式,然后用程序来读取*.csv的文件就搞定了。
好了,不说那么多没用的话了,直接入正题。以下是这两种方法的主要程序。
第一种方法源码:
文件名:access-mysql.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>列表</title>
</head>
<body>
<?php
$filename = "access-mysql.php?";
$pagesize=100; //设置每一页显示的记录数
$db = 'D:\web\text\xls_php\a.mdb';
$conn = new COM('ADODB.Connection');
$conn->Open("DRIVER={Driver do Microsoft Access (*.mdb)}; DBQ=$db");
$sql = 'select count(*) from Sheet1';
$res = $conn->Execute($sql);
while (!$res->EOF)
{
$numrows = $res->Fields[0]->Value;
$res->MoveNext();
}
$pages=intval($numrows/$pagesize);
if ($numrows % $pagesize){
$pages++;}
//判断页数设置
if (isset($_GET["page"]))
{
$page=intval($_GET["page"]);
}
else{
$page=1; //否则,设置为第一页
}
//计算记录偏移量
$offset=$pagesize*($page - 1);
//读取指定记录数
$sql="select * from Sheet1";
$res = $conn->Execute($sql);
echo $res->move($offset);
if (!$res->EOF)
{
$i=0;
?>
<table width="100%" border="1" cellspacing="0">
<tr><!--这一行是我需要的几个字段名称-->
<td width="3%">$n</td>
<td width="3%">id</td>
<td width="3%">wuli</td>
<td width="7%">bianhao</td>
<td width="9%">J</td>
<td width="4%">P</td>
<td width="4%">S</td>
<td width="7%">title</td>
<td width="9%">shen</td>
<td width="4%">jieshou</td>
<td width="5%">money</td>
<td width="21%">content</td>
</tr>
<?php
$i=0;
$n = ($page-1)*100;
if($n=($page-1) ."00")
$n = ($page-1)."00";
while (!$res->EOF) {
$n++;
$i++;
$id = $res->Fields["id"]->Value;
$snno = $res->Fields["wuli"]->Value;
$newid = $res->Fields["bianhao"]->Value;
$stepkey = $res->Fields["J"]->Value;
$secret1 = $res->Fields["P"]->Value;
$secret2 = $res->Fields["S"]->Value;
$memo = $res->Fields["title"]->Value;
$fax = $res->Fields["jieshou"]->Value;
$content = $res->Fields["content"]->Value;
$lianxiren = $res->Fields["shen"]->Value;
?>
<tr <?php if($i%2){echo "bgcolor='#ffffff'";}?> >
<td><?=$n?></td>
<td><?=$id?></td>
<td><?=$snno?></td>
<td><?=$newid?></td>
<td><?=$stepkey?></td>
<td><?=$secret1?></td>
<td><?php if($secret2!="") echo $secret2; else echo " ";?></td>
<td><?php if($memo!="") echo $memo; else echo " ";?></td>
<td><?php if($lianxiren!="") echo $lianxiren; else echo " ";?></td>
<td><?php if($fax!="") echo $fax; else echo " ";?></td>
<td><?php if($res->Fields["ru"]->Value!="") echo $res->Fields["ru"]->Value; else echo " ";?></td>
<td><?php if($content!="") echo $content; else echo " ";?></td>
</tr>
<?php
//以下是把读取的信息导入到MYSQL数据库中
mysql_connect("localhost","root","") or die ("连接数据库失败!");
mysql_select_db("test");
$inser_sql = "insert into stepentry(newid,ab,snno,stepkey,secret1,secret2,starttype,startdate,senddate,memo) values ('{$newid}','','{$snno}','{$stepkey}','{$secret1}','{$secret2}','','','','{$memo}')";
echo $inser_sql."<br />";
mysql_query($inser_sql);
if ($i>=$pagesize){break;}
$res->MoveNext();
}
}
$res->Close();
$conn->Close();
$res = null;
$conn = null;
?>
<tr>
<td colspan="16">
<?php
echo "<div align='center'>共有".$pages."页(".$page."/".$pages.")</br></br>";
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if ($page>1)
{
echo "<a href='".$filename."page=".$first."'>[首页]</a> ";
echo "<a href='".$filename."page=".$prev."'>[上一页]</a> ";
}
if ($page<$pages)
{
echo "<a href='".$filename."page=".$next."'>[下一页]</a> ";
echo "<a href='".$filename."page=".$last."'>[尾页]</a> ";
}
echo "<select name=\"select\" onchange=\"location='".$filename."&page='+this.options[this.selectedIndex].value\">";
for($i=1;$i<=$pages;$i++)
{
echo "<option value='$i'";
if ($i==$page){echo "selected='selected'";}
echo " >$i</option>";
}
echo "</div>";
?>
</select></tr>
</table>
</body>
</html>
第二种方法源码:
文件名称:insertsql.php
<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);//这是屏蔽错误信息的
$file = file('a.csv');
$num = count($file);
for($i=0;$i <$num;$i++)
{
list($id1, $id2,$id3,$id4) = explode( ', ',$file[$i],4);
//echo($id1. ' <br /> ');
//echo($id2. ' <br /> ');
//echo($id3. ' <br /> ');
//echo($id4. ' <br /> ');
//echo($id5. ' <br /> ');
$id=explode(',',$id1);//print_r($id);
$id[0]=str_replace(" ","",$id[0]);
if($id[0]!="" && $id[0]!="物理编号")
{
$sql = mysql_query("select * from test.stepentry where snno='$id[0]'",$link);
//$count = mysql_num_rows($sql);
if(mysql_num_rows($sql)==0)
{
$q = "INSERT INTO test.stepentry(newid,originid,snno,stepkey,secret1,secret2,starttype,startdate,senddate,memo,fromman,toman) values ('$id[1]','','$id[0]','$id[2]','$id[3]','$id[4]','','','','$id[5]','$id[6]','$id[8]')";
echo $q."<br />";
mysql_query($q,$link);
}
else
{
$q = "update test.stepentry set fromman='$id[6]',toman='$id[8]' where snno='$id[0]'";
echo $q."<br />";
mysql_query($q,$link);
}
}
}
exit;
?>