Linux下将excel数据导入到mssql数据库中的方法(2)


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_num<count($exc->worksheet['name']); $ws_num++ )
{
// print "<b>Worksheet: \"";
// if( $exc->worksheet['unicode'][$ws_num] ) {
// print uc2html($exc->worksheet['name'][$ws_num]);
// } else
// print $exc->worksheet['name'][$ws_num];
//
// print "\"</b>";
$ws = $exc->worksheet['data'][$ws_num];
//
//
// print "<tr><td>&nbsp;</td>\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 "<td class=index>&nbsp;";
//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 "<tr><td class=index>".($i+1)."</td>\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."<br>");
}
//$conn->Query($sql);
}
}
}
//导入完成删除文件
unlink($filename);

}


你注意没有,我把执行的那一行注释掉的,只要去掉注释就可以正确执行了!
所用到的代码打包下载

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3855738d41107ab9aeb281147515cf40.html