时间:2021-07-01 10:21:17 帮助过:18人阅读
if (trim($_POST["cmd"])=="upload")
{
$err_corr = "Unsupported format or file corrupted";
$excel_file_size;
$excel_file = $_FILES['excel_file'];
$uploadservername=$UploadAbsPath."tmpexcel/".$_FILES['excel_file']['name'];
echo($uploadservername);
if (!is_writeable($UploadAbsPath."tmpexcel/"))
{
echo "目录不可写!"; exit;
}
else
{
echo "目录可写!";
}
if (move_uploaded_file($_FILES['excel_file']['tmp_name'], $uploadservername))
{
echo("上传成功");
}
else
{
echo("上传失败");
}
$excel_file=$uploadservername;
//if( $excel_file )
// $excel_file = $_FILES['excel_file']['tmp_name'];
if( $excel_file == '' ) fatal("No file uploaded");
$exc = new ExcelFileParser("debug.log", ABC_NO_LOG);//ABC_NO_LOG ABC_VAR_DUMP);
//echo($excel_file."|");
$style = $_POST['style'];
if( $style == 'old' )
{
$fh = @fopen ($excel_file,'rb');
if( !$fh ) fatal("No file uploaded");
if( filesize($excel_file)==0 ) fatal("No file uploaded");
$fc = fread( $fh, filesize($excel_file) );
@fclose($fh);
if( strlen($fc) < filesize($excel_file) )
fatal("Cannot read file");
$time_start = getmicrotime();
$res = $exc->ParseFromString($fc);
$time_end = getmicrotime();
}
elseif( $style == 'segment' )
{
$time_start = getmicrotime();
$res = $exc->ParseFromFile($excel_file);
$time_end = getmicrotime();
}
switch ($res) {
case 0: break;
case 1: fatal("Can't open file");
case 2: fatal("File too small to be an Excel file");
case 3: fatal("Error reading file header");
case 4: fatal("Error reading file");
case 5: fatal("This is not an Excel file or file stored in Excel < 5.0");
case 6: fatal("File corrupted");
case 7: fatal("No Excel data found in file");
case 8: fatal("Unsupported file version");
default:
fatal("Unknown error");
}
/*
print '';';
print_r( $exc );
print '
exit;
*/
show_time();
echo <<Legend:
");
/* print "Formats
";
foreach($exc->format as $value) {
printf("( %x )",array_search($value,$exc->format));
print htmlentities($value,ENT_QUOTES);
print "
";
}
print "XFs
";
for( $i=0;$ixf['format']);$i++) {
printf ("(%x)",$i);
printf (" format (%x) font (%x)",$exc->xf['format'][$i],$exc->xf['font'][$i]);
print "
";
}
*/
}
运行效果如下:
第二步是要读取数据出来,代码如下:
代码如下:
if ($_POST["action"]=="do")
{
//处理数据
//先读取表头记录
$excel_file=$_POST["excel_file"];
$fh = @fopen ($excel_file,'rb');
$fc = fread( $fh, filesize($excel_file) );
@fclose($fh);
//echo("执行".$excel_file);
$exc = new ExcelFileParser("debug.log", ABC_NO_LOG);//ABC_NO_LOG ABC_VAR_DUMP);
//echo($excel_file."|");
$style = $_POST['style'];
if( $style == 'old' )
{
$fh = @fopen ($excel_file,'rb');
if( !$fh ) fatal("No file uploaded");
if( filesize($excel_file)==0 ) fatal("No file uploaded");
$fc = fread( $fh, filesize($excel_file) );
@fclose($fh);
if( strlen($fc) < filesize($excel_file) )
fatal("Cannot read file");
$time_start = getmicrotime();
$res = $exc->ParseFromString($fc);
$time_end = getmicrotime();
}
elseif( $style == 'segment' )
{
$time_start = getmicrotime();
$res = $exc->ParseFromFile($excel_file);
$time_end = getmicrotime();
}
switch ($res) {
case 0: break;
case 1: fatal("Can't open file");
case 2: fatal("File too small to be an Excel file");
case 3: fatal("Error reading file header");
case 4: fatal("Error reading file");
case 5: fatal("This is not an Excel file or file stored in Excel < 5.0");
case 6: fatal("File corrupted");
case 7: fatal("No Excel data found in file");
case 8: fatal("Unsupported file version");
default:
fatal("Unknown error");
}
//以及读取完毕,如果没有错误的话就可以循环往MSSQL中增加数据了!
for( $ws_num=0; $ws_numworksheet['name']); $ws_num++ )
{
// print "Worksheet: \"";
// if( $exc->worksheet['unicode'][$ws_num] ) {
// print uc2html($exc->worksheet['name'][$ws_num]);
// } else
// print $exc->worksheet['name'][$ws_num];
//
// print "\"";
$ws = $exc->worksheet['data'][$ws_num];
//
//
// print "\n";
$fkhxmnum=0;
$fsfzhnum=0;
$fyddhnum=0;
$ftxdznum=0;
$femailnum=0;
$flxdhnum=0;
$fkhahnum=0;
$fbzxxnum=0;
for( $j=0; $j<=$ws['max_col']; $j++ ) {
//print "";
//if( $j>25 ) print chr((int)($j/26)+64);
//先读取列名
$tmpcolum=trim($_POST["$j"]);
//echo($tmpcolum."|");
if ($tmpcolum=="fkhxm") $fkhxmnum=$j;
if ($tmpcolum=="fsfzh") $fsfzhnum=$j;
if ($tmpcolum=="fyddh") $fyddhnum=$j;
if ($tmpcolum=="ftxdz") $ftxdznum=$j;
if ($tmpcolum=="femail") $femailnum=$j;
if ($tmpcolum=="flxdh") $flxdhnum=$j;
if ($tmpcolum=="fkhah") $fkhahnum=$j;
if ($tmpcolum=="fbzxx") $fbzxxnum=$j;
}
for( $i=0; $i<=$ws['max_row']; $i++ ) {
//$fkhxm=
//echo($fkhxmnum.$fsfzhnum.$fyddhnum.$ftxdznum.$femailnum.$flxdhnum.$fkhahnum.$fbzxxnum);
//print "".($i+1)." \n";
if(isset($ws['cell'][$i]) && is_array($ws['cell'][$i]) ) {
if ($fkhxmnum!=0&&$ftxdznum!=0&&($fyddhnum!=0||$flxdhnum!=0))//请在这里指定必须的不为空的字段
{
$sql="insert into k_qlkhxx(fkhxm,fsfzh,fyddh,ftxdz,femail,flxdh,$fkhah,fbzxx,fglry,fglryxm,fdjry,ffzdm) values('".uc2html($exc->sst['data'][$ws['cell'][$i][$fkhxmnum]['data']])."','".$exc->sst['data'][$ws['cell'][$i][$fsfzhnum]['data']]."','".$exc->sst['data'][$ws['cell'][$i][$fyddhnum]['data']]."','".uc2html($exc->sst['data'][$ws['cell'][$i][$ftxdznum]['data']])."','".uc2html($exc->sst['data'][$ws['cell'][$i][$femailnum]['data']])."','".$exc->sst['data'][$ws['cell'][$i][$flxdhnum]['data']]."','".uc2html($exc->sst['data'][$ws['cell'][$i][$fkhahnum]['data']])."','".uc2html($exc->sst['data'][$ws['cell'][$i][$fbzxxnum]['data']])."','".$_SESSION["uyhmc"]."','".$_SESSION["uyhxm"]."','".$_SESSION["uyhmc"]."','".$_SESSION["ubm"]."')";
echo($sql."
");
}
//$conn->Query($sql);
}
}
}
//导入完成删除文件
unlink($filename);
}
你注意没有,我把执行的那一行注释掉的,只要去掉注释就可以正确执行了!
所用到的代码打包下载http://xiazai.jb51.net/201002/yuanma/php_excel_mysql.rar人气教程排行
- 174次 1 php如何获取跳转前的url
- 174次 2 php格林威治时间转换成当前时间的方法
- 174次 3 为什么php不能做大型系统?
- 174次 4 range函数怎么用
- 174次 5 php中计算页面加载时间几种方法总结_PHP教程
- 174次 6 求帮助,关于paypal支付返回值修改订单状态
- 174次 7 typecho怎么配置文章内容页?
- 174次 8 PhpStorm左侧structure不显示文件的方法列表是这么回事?
- 174次 9 查看PHP的环境变量_PHP
- 174次 10 PHP Primary script unknown 解决方法总结
- 174次 11 php的命名空间与自动加载实现方法
- 174次 12 解决laravel 出现ajax请求419(unknown status)的问题
- 173次 13 php 如何删除mysql记录
- 173次 14 PHP如何替换数组中的指定元素
- 173次 15 怎么去除字符串中非汉字、非字母、非数字的字符
- 173次 16 mysql如何一次执行多条SQL语句
- 173次 17 修改header里面的Connection为close解决方法
- 173次 18 PHP基于session.upload_progress 实现文件上传进度显示功能详解
- 173次 19 php5.6.x到php7.0.x特性小结
- 172次 20 php为什么会出现504错误