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