Linux下将excel数据导入到mssql数据库中的方法
前端之家收集整理的这篇文章主要介绍了
Linux下将excel数据导入到mssql数据库中的方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
先清理一下思路先,~~
首先:需要把文件上传到服务器上
然后:读取excel数据列显示出来
然后:让用户选择字段的对应关系
然后:提交数据,读取字段的对应关系
最后:批量导入数据,删除临时文件
一共是以上五步骤!我们一步步分析~~~
第一步:下载附件中的PHPexcelparser4.rar,这个文件是上传excel盗服务器上并以web形式展示出来的!这个一般没有问题的!问题是程序的做法是把表存为临时表而没有真正保存下来,所以首先要更改程序代码为
<div class="codetitle"><a style="CURSOR: pointer" data="49175" class="copybut" id="copybut49175" onclick="doCopy('code49175')"> 代码如下:
<div class="codebody" id="code49175">
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 <<<LEG
Legend: