Home > Backend Development > PHP Tutorial > How to import excel data into mssql database under Linux_PHP tutorial

How to import excel data into mssql database under Linux_PHP tutorial

WBOY
Release: 2016-07-21 15:41:23
Original
961 people have browsed it

Let’s clear up our ideas first,~~
First: upload the file to the server
Then: read the excel data column and display it
Then: let the user select the corresponding relationship of the fields
Then: submit Correspondence between data and read fields
Finally: import data in batches and delete temporary files
A total of the above five steps! Let’s analyze step by step~~~
Step 1: Download phpexcelparser4.rar in the attachment , this file is uploaded to the excel server and displayed in web form! This is generally no problem! The problem is that the program saves the table as a temporary table without actually saving it, so first change the program code to

Copy code The code is as follows:

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

show_time();

echo <<Legend:













Data typeDescription
 An empty cell
ABCabcString
12345Integer
123.45Float
123.45Date





LEG;
/*
print "
"; <br>print_r ($exc->worksheet); <br>print_r($exc->sst); <br>print "
";
*/
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; $j<=$ws['max_col']; $j++ ) {
print "";
}

print "n";
for( $j=0; $j<=$ws['max_col']; $j++ ) {
print "";
}

//Header output completed
if ($ws['max_row']>9)
{
$shownum=9;
}
else
{
$shownum=$ws['max_row'];//Only output the first 10 pieces of data
}
for( $i=0; $i<=$shownum; $ i++ ) {
print "n";
if(isset($ws['cell' ][$i]) && is_array($ws['cell'][$i]) ) {
for( $j=0; $j<=$ws['max_col']; $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; $j<=$ws['max_col']; $j++ )
print "";
print "n";
}
print "n";
}

echo "
";
if( $j> 25) print chr((int)($j/26)+64);
//A drop-down list is displayed here to display data
//Note that it is circular data

echo("n");
print "
";
if( $j>25 ) print chr((int)($j/26)+64);
print chr(($j % 26) + 65)." Column name
".($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 "
";
}
*/
}

The operation effect is as follows:
How to import excel data into mssql database under Linux_PHP tutorial
The second step is to read the data, the code is as follows:
Copy code Code As follows:

if ($_POST["action"]=="do")
{
//Processing data
//Read the header record first
$excel_file=$_POST["excel_file"];
$fh = @fopen ($excel_file,'rb');
$fc = fread( $fh, filesize($excel_file) );
@fclose($fh);
//echo("execute".$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");
}
//and reading completed , if there are no errors, you can loop to add data to 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);
//Read the column name first

$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)."< ;/td>n";
if(isset($ws['cell'][$i]) && is_array($ws['cell'][$i]) ) {
if ($fkhxmnum !=0&&$ftxdznum!=0&&($fyddhnum!=0||$flxdhnum!=0))//Please specify the required non-empty fields here
{
$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);
}
}
}
//Delete file after importing
unlink($filename);

}

Did you notice that I commented out the line of execution? As long as you remove the comment, it can be executed correctly!
The code used is packaged and downloaded from http://xiazai.jb51.net/201002/yuanma/php_excel_mysql .rar

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321150.htmlTechArticleLet’s clear up the ideas first,~~ First: you need to upload the file to the server and then: read the excel data column Display it and then: let the user select the corresponding relationship of the fields and then: submit the data, read the words...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template