日期:2014-05-17 浏览次数:20494 次
<?php
include_once("reader.php");
include_once("conn.php");
$remotefilename= date("Ymdhis").".xls";
if (substr($_FILES["file"]["name"],-4)==".xls")
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"xlstmp/" .$remotefilename)==1)
{
//上传成功并开始读取xls文件
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP936');
$data->read("xlstmp/" .$remotefilename);
$count=0;
if($data->sheets[0]['numRows']>=2 && $data->sheets[0]['numCols']>=2)
{
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
{
$s="";
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
{
if(strlen($data->sheets[0]['cells'][$i][$j])==0) break;
$s.= $data->sheets[0]['cells'][$i][$j]."|";
}
if(strlen($s)!=0)
{
$count++;
$ss =explode('|',$s);//将关键词以空格分割到数组中。
// $sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\") VALUES('1111','ok')";
//执行上面这句都是可以的
$sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\") VALUES('".$ss[0]."','".$ss[1]."')";
//执行上面这句就不成功。
// $sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\") VALUES('".$data->sheets[0]['cells'][$i][1]."','".$data->sheets[0]['cells'][$i][2]."')";
echo $sql."<br>";
$result = $PG -> query($sql);
}
}
echo $count."records";
}
else
{
echo "xls's rows or columns is so few.";
}
}
else
echo "upload failed.";
}
}
else
{
echo "invalid file";
}
$PG ->close();
?>