国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > htmlcss > php上传xls文件导入到mysql数据表

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

来源:程序员人生   发布时间:2015-01-17 09:43:02 阅读次数:3431次

申明下面的文章属于转载,现在大家新下载的版本跟下面列子这个不1样!我看了很多网上的列子,最后我还是读了那个英文的手册,上面说的很清楚!大家英语不好可以下载有道!其实重点是要包括下面两行:

  require_once 'excellib/PHPExcel.php';  
         require_once 'excellib/PHPExcel/IOFactory.php';

 你们各自的替换各自寄存目录,我这里用的是excellib目录。就是要把PHPExcel.php',PHPExcel/IOFactory.php包括进来就能够,其他的跟后面这个例子差不多。

本功能实际上是通过1个国外php对xls文件读取的类实现的,网上的资料多是excel文件另存为csv文件,然后从csv文件导入。

    PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader  下载解压后,

目录结构

php上传xls文件导入到mysql数据表 - 狮子吼 - 正念

首先我们需要1个上传页面:

  php上传xls文件导入到mysql数据表 - 狮子吼 - 正念

代码以下:

<!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⑻');

$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)⑵;

    $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表

php上传xls文件导入到mysql数据表 - 狮子吼 - 正念

测试环境 windows xp

         phpnow 1.4

  地址:http://localhost/test/up.php

测试图:

php上传xls文件导入到mysql数据表 - 狮子吼 - 正念

php上传xls文件导入到mysql数据表 - 狮子吼 - 正念

本文出自 “成长流水账----开源世界,分享知识的快乐!” 博客,请务必保存此出处http://jason2016.blog.51cto.com/892969/289411本文出自 51CTO.COM技术博客

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生