Linux下将excel数据导入到mssql数据库中的方法_PHP教程

WBOY
Freigeben: 2016-07-21 15:41:23
Original
942 Leute haben es durchsucht

先清理一下思路先,~~
首先:需要把文件上传到服务器上
然后:读取excel数据列显示出来
然后:让用户选择字段的对应关系
然后:提交数据,读取字段的对应关系
最后:批量导入数据,删除临时文件
一共是以上五步骤!我们一步步分析~~~
第一步:下载附件中的phpexcelparser4.rar ,这个文件是上传excel盗服务器上并以web形式展示出来的!这个一般没有问题的!问题是程序的做法是把表存为临时表而没有真正保存下来,所以首先要更改程序代码为

复制代码 代码如下:

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) 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 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 '
'; <br>print_r( $exc ); <br>print '
Nach dem Login kopieren
';
exit;
*/

show_time();

echo Legend:













Data type Description
  An empty cell
ABCabc String
12345 Integer
123.45 Float
123.45 Date





LEG;
/*
print "
"; <br>print_r ($exc->worksheet); <br>print_r($exc->sst); <br>print "
Nach dem Login kopieren
";
*/
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];

if( is_array($ws) &&
isset($ws['max_row']) && isset($ws['max_col']) ) {
echo "\n

\n";

print "\n";
for( $j=0; $jprint "";
}

print "\n";
for( $j=0; $jprint "";
}

//表头输出完毕
if ($ws['max_row']>9)
{
$shownum=9;
}
else
{
$shownum=$ws['max_row'];//只输出前10条数据
}
for( $i=0; $iprint "\n";
if(isset($ws['cell'][$i]) && is_array($ws['cell'][$i]) ) {
for( $j=0; $j
if( ( is_array($ws['cell'][$i]) ) &&
( isset($ws['cell'][$i][$j]) )
){

// print cell data
print "\n";
} else {
print "\n";
}
}
} else {
// print an empty row
for( $j=0; $jprint "";
print "\n";
}
print "\n";
}

echo "
  ";
if( $j>25 ) print chr((int)($j/26)+64);
//这里要显示一个下拉列表来显示数据
//注意是循环数据

echo("\n");
print "
  ";
if( $j>25 ) print chr((int)($j/26)+64);
print chr(($j % 26) + 65)." 列名
".($i+1)."$data = $ws['cell'][$i][$j];

$font = $ws['cell'][$i][$j]['font'];
$style = " style ='".ExcelFont::ExcelToCSS($exc->fonts[$font])."'";

switch ($data['type']) {
// string
case 0:
print "dt_string\"".$style.">";
$ind = $data['data'];
if( $exc->sst['unicode'][$ind] ) {
$s = uc2html($exc->sst['data'][$ind]);
} else
$s = $exc->sst['data'][$ind];
if( strlen(trim($s))==0 )
print " ";
else
print $s;
break;
// integer number
case 1:
print "dt_int\"".$style."> ";
print $data['data'];
break;
// float number
case 2:
print "dt_float\"".$style."> ";
echo $data['data'];
break;
// date
case 3:
print "dt_date\"".$style."> ";

$ret = $data[data];//str_replace ( " 00:00:00", "", gmdate("d-m-Y H:i:s",$exc->xls2tstamp($data[data])) );
echo ( $ret );
break;
default:
print "dt_unknown\"".$style.">  ";
break;
}
print "
  

\n";
} else {
// emtpty worksheet
print " - empty
\n";
}
print "
";

}
echo("");
echo("");
/* 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 "
";
}
*/
}

运行效果如下:
 Linux下将excel数据导入到mssql数据库中的方法_PHP教程
第二步是要读取数据出来,代码如下:
复制代码 代码如下:

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) 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 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//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//$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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/321150.htmlTechArticle先清理一下思路先,~~ 首先:需要把文件上传到服务器上 然后:读取excel数据列显示出来 然后:让用户选择字段的对应关系 然后:提交数据,读取字...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage