php上傳xls文件導入到mysql數據表


2010-03-30 12:36:42
標籤:php mysql 數據 xls 文件
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://jason2016.blog.51cto.com/892969/289411
 
    聲明:本文是本人從網絡整理髮布,旨在分享知識,感謝原文作者,歡迎轉載,請保留連接,謝謝!


   本功能實際上是通過一個國外php對xls文件讀取的類實現的,網上的資料多是excel文件另存爲csv文件,然後從csv文件導入。


    PHP-ExcelReader,下載地址: http://sourceforge.net/projects/phpexcelreader  下載解壓後,在文件夾Excel中的oleread.inc代碼合併到reader.php,將紅色字體行註釋

//require_once 'PEAR.php';
require_once 'Spreadsheet/Excel/Reader/OLERead.php';
//require_once 'OLE.php';

註釋後
//require_once 'PEAR.php';
//require_once 'Spreadsheet/Excel/Reader/OLERead.php';
//require_once 'OLE.php';


目錄結構


首先我們需要一個上傳頁面:
  
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>導入測試</title> 
</head> 

<body> 

<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="#">小學數學題目數據導入</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> 
</body> 
</html> 

數據庫連接代碼頁:

<?php 
$host="localhost"; 
$user="root"; 
$password="123456"; 
$database="project"; 
$connect=@mysql_connect("$host","$user","$password"); 
if(!$connect) 

  echo "database connect wrong"; 
  exit; 
  } 
$db=mysql_select_db("$database",$connect); 
$sql=mysql_query("SET NAMES 'gb2312'"); 
?> 



讀取插入的頁面

代碼如下:

<?php 
error_reporting(E_ALL ^ E_NOTICE); 
if($_POST){ 
$Import_TmpFile = $_FILES['file']['tmp_name']; 
require_once 'conn.php'; 
mysql_select_db('test_xls'); //選擇數據庫    
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){    
         $Isql = "Select id from    xls    where id='".$tmp[1]."'"; 
         $sql = "Insert into xls (id,tm) value("; 
         $sql.="'".$tmp[1]."','".$tmp[2]."')"; 
     
        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;        
    } 
?> 

數據庫testz_xls表




測試環境 windows xp
         phpnow 1.4
  地址:http://localhost/test/up.php


測試圖:





本文出自 “B612號小行星” 博客,請務必保留此出處http://jason2016.blog.51cto.com/892969/289411

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章