How to import excel data to the mssql database in Linux _ PHP Tutorial

Source: Internet
Author: User
Tags unsupported
How to import excel data to the mssql database in Linux. First, clear your ideas. first ,~~ First, you need to upload the file to the server and then: Read the excel data column to display it, and then: let the user select the corresponding relationship of the field and then: submit data, read the word first, clear the idea first ,~~
First, you need to upload the file to the server.
Then: Read the excel data column and display it
Then, let the user select the corresponding field relationship.
Then: submit the data and read the correspondence between fields.
Finally, import data in batches and delete temporary files.
The above five steps are in total! Step by Step Analysis ~~~
Step 1: Download phpexcelparser4.rar in the attachment. This file is uploaded to the excel stealing server and displayed on the web! This is generally okay! The problem is that the program saves the table as a temporary table but does not actually save it. Therefore, you must first change the program 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 "directory cannot be written! "; Exit;
}
Else
{
Echo "directory writable! ";
}
If (move_uploaded_file ($ _ FILES ['Excel _ file'] ['tmp _ name'], $ uploadservername ))
{
Echo ("uploaded successfully ");
}
Else
{
Echo ("Upload failed ");
}
$ 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 already upted ");
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; $ I Xf ['format']); $ I ++ ){
Printf ("(% x)", $ I );
Printf ("format (% x) font (% x)", $ exc-> xf ['format'] [$ I], $ exc-> xf ['font'] [$ I]);

Print"
";
}
*/
}


The running effect is as follows:

The second step is to read the data. the code is as follows:

The code is as follows:


If ($ _ POST ["action"] = "do ")
{
// Process 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 already upted ");
Case 7: fatal ("No Excel data found in file ");
Case 8: fatal ("Unsupported file version ");

Default:
Fatal ("Unknown error ");
}
// After reading, you can add data to MSSQL cyclically if there is no error!
For ($ ws_num = 0; $ ws_num Worksheet ['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 )."\ N ";
If (isset ($ ws ['cell '] [$ I]) & is_array ($ ws ['cell'] [$ I]) {
If ($ fkhxmnum! = 0 & $ ftxdznum! = 0 & ($ fyddhnum! = 0 | $ flxdhnum! = 0) // specify a required non-empty field 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 );
}
}
}
// The file is deleted after the import.
Unlink ($ filename );

}


You do not know. I have commented out the line for execution. you only need to remove the comment and you will be able to correctly execute it!
The code used to package download http://xiazai.jb51.net/201002/yuanma/php_excel_mysql.rar

Bytes ,~~ First, you need to upload the file to the server and then: Read the excel data column to display it, then: let the user select the corresponding relationship of the field and then: submit data, read the word...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.