废话少说,先上效果图:
// 上传之后的效果如下图///
================= test.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>
<title>iframe tag test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form name="form2" method="post" enctype="multipart/form-data">
<input type="hidden" name="leadExcel" value="true">
<table align="center" width="90%" border="0">
<tr>
<td>
<input type="file" name="inputExcel"><input type="submit" value="导入数据">
</td>
</tr>
</table>
</form>
<div id="guest">++++客人名单展示区++++</div>
<p> </p>
</body>
</html>
<? require("inc_func.php"); ?>
<?
if($_POST['leadExcel'] == "true")
{
//获取上传的文件名
$filename = $HTTP_POST_FILES['inputExcel']['name'];
//上传到服务器上的临时文件名
$tmp_name = $_FILES['inputExcel']['tmp_name'];
$aaa = uploadFile($filename,$tmp_name);
echo "<script>document.getElementById('guest').innerHTML ='".$aaa."';</script>";
}
?>
================== inc_func.php代码==============================
<?
//导入Excel文件
function uploadFile($file,$filetempname)
{
//自己设置的上传文件存放路径
$filePath = 'upFile/';
$str = "";
//下面的路径按照你PHPExcel的路径来修改
set_include_path('.'. PATH_SEPARATOR .
'/www' . PATH_SEPARATOR .
get_include_path());
include "tool/phpexcel/PHPExcel/Writer/IWriter.php";
include "tool/phpexcel/PHPExcel/Writer/Excel5.php";
require_once 'tool/phpexcel/PHPExcel.php';
include 'tool/phpexcel/PHPExcel/IOFactory.php';
$filename=explode(".",$file);//把上传的文件名以"."好为准做一个数组。
$time=date("y-m-d-H-i-s");//去当前上传的时间
$filename[0]=$time;//取文件名t替换
$name=implode(".",$filename); //上传后的文件名
$uploadfile=$filePath.$name;//上传后的文件名地址
//move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
$result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
if($result) //如果上传文件成功,就执行导入excel操作
{
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($uploadfile);
///
$PHPExcel = $objReader->load($uploadfile);
$sheet = $PHPExcel->getSheet(0);/**读取excel文件中的第一个工作表*/
$highestColumn = $sheet->getHighestColumn();/**取得最大的列号*/
$highestRow = $sheet->getHighestRow();/**取得一共有多少行*/
$bgdata ="";
$i=1;//行数
for($currentRow = 2;$currentRow <= $highestRow;$currentRow++){
/**从第A列开始输出*/
$k=1;//列数
for($currentColumn= 'A';$currentColumn<= $highestColumn; $currentColumn++){
$val = $sheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数*/
/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
$biaoge = iconv('utf-8','gb2312', $val);
switch($k){
case "1":
$bgdata.="<input name=\"plan_user_name[]\" type=\"text\" id=\"plan_user_name[]\" value=\"$biaoge\" size=\"9\">";break;
case "2":
$bgdata.="<select name=\"plan_user_sex[]\" id=\"plan_user_sex[]\"><option value=\"$biaoge\">$biaoge</option><option value=\"男\">男</option><option value=\"女\">女</option></select>";break;
case "3":
$bgdata.="<select name=\"plan_user_sort[]\" id=\"plan_user_sort[]\"><option value=\"$biaoge\">$biaoge</option><option value=\"身份证\">身份证</option><option value=\"学生证\">学生证</option><option value=\"老年人证\">老年人证</option><option value=\"军官证\">军官证</option><option value=\"驾驶证\">驾驶证</option><option value=\"其它\">其它</option></select>";break;
case "4":
$bgdata.="<input type=\"text\" name=\"plan_user_haoma[]\" id=\"plan_user_haoma[]\" value=\"$biaoge\">";break;
case "5":
$bgdata.="<input name=\"plan_user_tel[]\" type=\"text\" id=\"plan_user_tel[]\" size=\"13\" value=\"$biaoge\">";break;
}
$k++;
}//列循环
$bgdata.="</br>";
$i++;
}//行循环
unlink($uploadfile); //删除上传的excel文件
return $bgdata;
}
else{$msg = "导入失败!";}
}
?>
本来想把execel模板表添加到附件中的,但是csdn 无法上传,所以只能截图了,大家对照着个截图来上传模板表吧!