Home >Backend Development >PHP Tutorial >PHP import execel[xls] data table to mysql_PHP tutorial
<script><br>
function import_check(){<br>
var f_content = form1.file.value;<br>
var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length)<br>
Fileext=fileext.toLowerCase()<br>
If (fileext!='.xls')<br>
{<br>
alert("Sorry, the imported data format must be an xls format file. Please adjust the format and upload it again, thank you!"); <br>
Return false;<br>
}<br>
}<br>
</script>
if($_POST){
$Import_TmpFile = $_FILES['file']['tmp_name'];
require_once '../../inc/connect.php';
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('GB2312');
$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 inte_card from gx_integral where inte_card='".$tmp[2]."'";
$sql = "Insert into gx_integral(inte_name,inte_card,inte_status,inte_integral,inte_date,inte_date2,inte_tel) value(";
$sql.="'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".TtoD($tmp[5])."','".TtoD($tmp[6])."','".$tmp[7]."')";
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;
}
?>
这里是用到Spreadsheet_Excel_Reader组件了,那read.php文件我就不附上了,自己下一个吧,好了最后申明本站原创转载请注明: www.111cn.cn