Phpexcelreader read Excel automatically generate table and field

Source: Internet
Author: User
Phpexcelreader reading Excel automatically generate tables and fields
This is primarily implemented using Phpexcelreader, which generates table names and fields based on the file name of Excel and the first line of Excel, and the data is imported sequentially from the second line.



Phpexcelreader Download Address: http://sourceforge.net/projects/phpexcelreader/

Download down there is a "phpexcelreader.zip" of the compressed package. Unzip, we need only the contents of the Excel folder, first change "Oleread.inc" to "oleread.inc.php"

Modify the "reader.php" file

Modify the 31st line require_once ' spreadsheet/excel/reader/oleread.php ';
Instead ": require_once ' oleread.inc.php ';
The No. 261 line =& to = number is OK

Source code and sample files, as well as the process are in the download file!
I am a novice php, the wrong place please understand.

Upload File page!

 
  Excel Management
 
  


Upload file processing

 
  0) {switch ($_files [' upfile '] [' ERROR ']) {Case 1: $errorMsg = "Upload file exceeds limit! "; Break;case 2: $ERRORMSG =" The upload file exceeds the foreground specified size! "; Break;case 3: $ERRORMSG =" The upload file is incomplete! "; Break;case 4: $ERRORMSG =" No files uploaded! "; break;} echo $ERRORMSG;} else {if (! Is_dir (' upload ')) {mkdir (' upload ');} Determine if the file format is correct $imgtype = Array (' Application/octet-stream ', ' application/vnd.ms-excel '); if (! In_array ($_files [' Upfile ' [' type '], $imgType) {echo $_files [' upfile '] [' type ']. "Non-conforming file type"; exit ();} if (Is_uploaded_file ($_files [' upfile '] [' tmp_name ']) {$toFileName = ' upload/'. $_files [' upfile '] [' name '];//move temporary files to Specify the folder if (Move_uploaded_file ($_files [' upfile '] [' tmp_name '], $toFileName) {$excelFileName = $toFileName;} else {echo " File upload failed! ";}} else {echo ' is not uploading files! ";}}} else {echo "Please select Upload file! ";} ? >


Parse file assembly SQL

 "; require_once ' reader.php '; $data = new Spreadsheet_excel_reader (); $data->setoutputencoding (' UTF8 ');  Set the username and password of the database connection $conn= mysql_connect (' localhost ', ' root ', ' root ') or Die ("Cannot connect to the database!."); Set the encoding mysql_query ("Set names ' UTF8 '");//Database name mysql_select_db (' Excel '); echo "Whether file =". Is_file ($excelFileName). "
"If ($_post[' button '] && is_file ($excelFileName)) {$data->read ($excelFileName); $str = Explode (". ", $ Excelfilename); $strname = Explode ("/", $str [0]); $tablename = $strname [1];echo "Table name:". $tablename. "
"; $existsTable = existstable ($tablename);//echo $existsTable."
";//echo print_r ($data->sheets[0][' cells '][1])."
"; echo" "; $fieldCount = count ($data->sheets[0][' cells '][1]); if ($existsTable = = 0) {$sql =" CREATE table if not exists ". $tablename." (ID int primary key auto_increment, "; for ($i = 1; $i <= $fieldCount; $i + +) {if (!empty ($data->sheets[0][' cells '][1][$ I]) {$sql. = $data->sheets[0][' cells '][1][$i]. "varchar (255) NULL,";}} Remove the last comma $sql = substr ($sql, 0,strlen ($sql)-1); $sql. = ")"; echo "CREATE Table Sql=". $sql. "
"; $query =mysql_query ($sql); if ($query) {$sign = 1; echo "created the table successfully!"
"; }else{$sign = 0; echo "Failed to create TABLE!"
"; Return if ($sign = = 1) {for ($i = 2; $i <= $data->sheets[0][' numrows '); $i + +) {/*print_r ($data->sheets [0] [' Cells '] [$i]); echo "
"; */$sql =" INSERT into ". $tablename." VALUES (null, "; for ($j = 1; $j <= count ($data->sheets[0][' cells ' [$i]); $j + +) {$sql. = "'". $data->sheets[0][' cells '] [$i] [$j]. "'"., ";} Remove the last comma $sql = substr ($sql, 0,strlen ($sql)-1), $sql. = ")", echo "Insert Table Sql=". $sql. "
"; $query = mysql_query ($sql); if ($query) {echo" Insert table successfully!
"; }else{echo "Insert Table failed!"
"; Break }}}}} else {for ($i = 2; $i <= $data->sheets[0][' numrows '); $i + +) {/*print_r ($data->sheets[0][' cel ls '] [$i]); echo "
"; */$sql =" INSERT into ". $tablename." VALUES (null, "; for ($j = 1; $j <= count ($data->sheets[0][' cells ' [$i]); $j + +) {$sql. = "'". $data->sheets[0][' cells '] [$i] [$j]. "'"., ";} Remove the last comma $sql = substr ($sql, 0,strlen ($sql)-1), $sql. = ")", echo "Insert Table Sql=". $sql. "
"; $query = mysql_query ($sql); if ($query) {echo" Insert table successfully!
"; }else{echo "Insert Table failed!"
"; Break }}}echo "";} function InsertData () {}//looks for the existence of table function existstable ($tablename) {$sql = "select * from". $tablename; $query =mysql_ Query ($sql); if ($query) {return 1;} Else{return 0;}}? >
  • 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.