PHP+PHPEXCELREADER+MYSQ实现EXCEL表批量导入MYSQL

来源:互联网 发布:软件质量管理体系流程 编辑:程序博客网 时间:2024/05/19 22:52

这个功能,主要是为了把大气监测数据批量导入MYSQL数据库而写的。其实,只要把insert.php文件中的数据库选项,就可以添加其他的数据了。这里提供的是一种原理。

up.php选择文件上传。PS:要实现此功能,还需要下载phpexcelreader源代码。GOOGLE一下,你懂的。

<script>function import_check(){<!--上传文件用的-->    var f_content = form1.file.value;    var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length)        fileext=fileext.toLowerCase()     if (fileext!='.xls')        {         alert("对不起,导入数据格式必须是xls格式文件哦,请您调整格式后重新上传,谢谢!");                    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-data" method="post" action="insert.php">            <tr >            <td height="28" colspan="2" background="../skins/top_bg.gif"><label>  <strong><a href="#">乡镇PM10数据导入,慎用</a></strong></label></td>        </tr>        <tr>            <td width="18%" height="50"> 选择你要导入的数据表</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="导入数据"    onclick="import_check();"/>                </label> </td>        </tr>        <tr>            <td colspan="2" bgcolor="#DDF0FF">  [<span class="STYLE1">注</span>]数据导入格式说明:</td>        </tr>        <tr>            <td colspan="2">    1、其它.导入数据表文件必须是<strong>execel</strong>文件格式{.<span class="STYLE2">xls</span>}为扩展名.</td>        </tr>        <tr>            <td colspan="2">  2、execel文件导入数据顺序必须严格按照规定顺序。</td>        </tr>        <tr>            <td colspan="2"> </td>        </tr></form>    </table>



insert.php往数据库写入数据

<?php //往数据库中写入数据error_reporting(E_ALL ^ E_NOTICE);if($_POST){$Import_TmpFile = $_FILES['file']['tmp_name'];require_once '../configywcl.php'; require_once 'Excel/reader.php';$data = new Spreadsheet_Excel_Reader();$data->setOutputEncoding('UTF-8');$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){  //echo $tmp[1];//$tmp[1]=date('Y-m-d',$tmp[1]);         $sql = "Insert into xzdq (rq,nian,yue,ri,xl,gc,cy,yc,dd,cg,sl,hch,llh,qlh,lx,zkd,cgou,dsw,zf,sd,pw,sjy,das,xyl,fzz,nj,hb,fqgc,gd,df,rqzx,bz) value('$tmp[1]','$tmp[2]','$tmp[3]','$tmp[4]','$tmp[5]','$tmp[6]','$tmp[7]','$tmp[8]','$tmp[9]','$tmp[10]','$tmp[11]','$tmp[12]','$tmp[13]','$tmp[14]','$tmp[15]','$tmp[16]','$tmp[17]','$tmp[18]','$tmp[19]','$tmp[20]','$tmp[21]','$tmp[22]','$tmp[23]','$tmp[24]',   '$tmp[25]','$tmp[26]','$tmp[27]','$tmp[28]','$tmp[29]','$tmp[30]','$tmp[31]','$tmp[32]')";//echo $sql."<br>";                     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;           }?>