PHP import Excel into the MySQL database, using Excelparser

Source: Internet
Author: User
Tags ord mysql database

Class Excelparser
{
Private $_data=array (0, "");
Private $_excel_handle;
Private $_excel=array ();
/**
* Constructor
* @param <string> $filename file Temporary file name
*/
Public function __construct ($filename)
{
/**
* Introduction of Excelparser Class
* Common method is
* Requires path. ' Excelparser.php ';
*/
Import (' @. Util.PHPExcelParser.excelparser ', ', '. php ');
$this->_excel_handle=new Excelfileparser ();
Error getting
$this->checkerrors ($filename);
}
/**
* ERROR Check
*/
Private Function Checkerrors ($filename)
{
/**
* Method One
*/
$error _code= $this->_excel_handle->parsefromfile ($filename);
/**
* Method II
* $file _handle = fopen ($this->_filename, ' RB ');
* $content = fread ($file _handle,filesize ($this->_filename));
* Fclose ($file _handle);
* $error _code = $this->_excel->parsefromstring ($content);
* Unset ($content, $file _handle);
*/
Switch ($error _code)
{
Case 0:
No errors not handled
Break
Case 1:
$this->_data=array (1, ' File read error (Linux note read-write permission) ');
Break
Case 2:
$this->_data=array (1, ' file too small ');
Break
Case 3:
$this->_data=array (1, ' www.111cn.net reads Excel table header failed ');
Break
Case 4:
$this->_data=array (1, ' file read error ');
Break
Case 5:
$this->_data=array (1, ' file may be empty ');
Break
Case 6:
$this->_data=array (1, ' incomplete file ');
Break
Case 7:
$this->_data=array (1, ' Read data error ');
Break
Case 8:
$this->_data=array (1, ' version error ');
Break
}
unset ($error _code);
}
/**
* Excel Information Acquisition
*/
Private Function Getexcelinfo ()
{
if (1== $this->_data[0]) return;
/**
* Access to Sheet quantity
* Get the row and column corresponding to the sheet unit
*/
$this->_excel[' Sheet_number ']=count ($this->_excel_handle->worksheet[' name '));
For ($i =0 $i < $this->_excel[' Sheet_number '); $i + +)
{
/**
* Row to Column
* Note: Count starting from 0
*/
$row = $this->_excel_handle->worksheet[' data '] [$i] [' Max_row '];
$col = $this->_excel_handle->worksheet[' data '] [$i] [' Max_col '];
$this->_excel[' row_number ' [$i]= ($row ==null) 0:++ $row;
$this->_excel[' Col_number ' [$i]= ($col ==null) 0:++ $col;
Unset ($row, $col);
}
}
/**
* Chinese processing function
* @return <string>
*/
Private Function uc2html ($STR)
{
$ret = ';
For ($i =0 $i <strlen ($STR)/2; $i + +)
{
$charcode = Ord ($str [$i *2]) +256*ord ($str [$i *2+1]);
$ret. = ' &# '. $charcode. ';
}
Return mb_convert_encoding ($ret, ' UTF-8 ', ' html-entities ');
}
/**
* Excel Data acquisition
*/
Private Function Getexceldata ()
{
if (1== $this->_data[0]) return;

Modify Markup
$this->_data[0]=1;
Get Data
For ($i =0 $i < $this->_excel[' Sheet_number '); $i + +)
{
/**
* Loop to Line
*/
for ($j =0; $j < $this->_excel[' row_number '] [$i]; $j + +)
{
/**
* Loop to Column
*/
for ($k =0; $k < $this->_excel[' Col_number '] [$i]; $k + +)
{
/**
* Array (4) {
* [' type '] => type [0 character type 1 integer 2 floating-point number 3rd]
* ["Font"] => font
* ["Data"] => data
*   ...
* }
*/
$data = $this->_excel_handle->worksheet[' data ' [$i] [' cell '] [$j] [$k];
Switch ($data [' type '])
{
Case 0:
Character type
if ($this->_excel_handle->sst[' Unicode '] [$data [' Data ']])
{
Chinese processing
$data [' data '] = $this->uc2html ($this->_excel_handle->sst[' data '] [$data [' Data ']]);
}
Else
{
$data [' data '] = $this->_excel_handle->sst[' data '] [$data [' data ']];
}
Break
Case 1:
Integer
Todo
Break
Case 2:
Floating point numbers
Todo
Break
Case 3:
Date
Todo
Break
}
$this->_data[1][$i] [$j] [$k]= $data [' Data '];
Unset ($data);
}
}
}
}
/**
* Main function
* @return <array> Array (identifier, content s)
*/
Public Function Main ()
{
Excel Information Acquisition
$this->getexcelinfo ();
Excel Data acquisition
$this->getexceldata ();
return $this->_data;
}
}

?>

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.