php上传xls文件导入到mysql数据表

来源:互联网 发布:java redis lua 编辑:程序博客网 时间:2024/06/07 03:23

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

原创粉丝点击