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';
//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>
<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'");
?>
$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
- php上传xls文件导入到mysql数据表
- php上传xls文件导入到mysql数据表
- PHP上传Excel文件导入数据到MySQL数据库示例
- 怎么将xls文件导入到MySQL数据库
- PHP把excel(xls)文件数据导入mysql数据库
- 根据数据表导入xls数据
- [Sqoop]将Mysql数据表导入到Hive
- 使用sqlyog导入数据表到MySql中
- 将csv导入到mysql数据表中
- 从文件导入数据到数据表
- 通过文件导出导入数据到数据表
- SQL server 导入csv文件到数据表
- xls 导入 mysql
- mysql 数据库、数据表文件导入导出
- php 导出 xls,csv 的方法,csv导入mysql
- vbscript写xls文件里面数据导入到数据库中
- 如何将xls文件导入到mongoDB中?
- xls文件上传
- 分布式爬虫nutch2.0初体验------三大亮点
- 新旅程的开启
- rails3 数据格式
- Cisco ASA Firewall导致exchange 2010 EDGE邮件不能传输出现错误
- Android蓝牙开发浅析
- php上传xls文件导入到mysql数据表
- OGRE+CEGUI游戏教程(5)--物品/装备和技能系统
- FacebookIPO后还能保持节操么 Bergus的故事是一个线索
- MySQl心得4--3--数据库查询3--from字句
- 宏基,华硕,东芝发布Win 8平板抗衡iPad
- MySQl心得4--4--数据库查询4--子查询
- Win7移动用户文件夹的一个简便方法
- Daily used tables, views in ORACLE SQL
- 让PIL生成的字带有描边效果