php匯入execel[xls]資料表到mysql

來源:互聯網
上載者:User

<style type="text/css">
<!--
@import url("../style/admin.css");
.STYLE1 {
 color: #FF0000;
 font-weight: bold;
}
.STYLE2 {color: #0033FF}
-->
</style>
<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="">
 
    <tr >
      <td height="28" colspan="2" background="../skins/top_bg.gif"><label> &nbsp;<strong><a href="#">紅利卡積分會員資料匯入</a></strong></label></td>
    </tr>
    <tr>
      <td width="18%" height="50">&nbsp;選擇你要匯入的資料表</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>
&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2" bgcolor="#DDF0FF"> &nbsp;[<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">&nbsp;&nbsp;2、execel檔案匯入資料順序必須如:會員名稱  | 會員卡號  | 身份證  | 積份  | 註冊時間  | 到期日期  | 聯絡電話號碼 如下圖:</td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;<img src="pic/1.jpg" width="758" height="248" /></td>
    </tr></form>
  </table>

<?php
error_reporting(E_ALL ^ E_NOTICE);
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.net
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.