Excel data acquisition demonstration< ;/title>
Excel data acquisition demonstration
< ;/html>
/**
* CopyRight (c) 2009,
* All rights reserved.
* File name: excel data acquisition
* Summary:
*
* @author Monday [url=mailto :ixqbar@hotmail.com]ixqbar@hotmail.com[/url]
* @version 0.1
*/
class ExcelParser
{
private $_data=array(0,'');
private $ _excel_handle;
private $_excel=array();
/**
* Constructor
* @param
$filename Temporary file name of uploaded file
*/
public function __construct($filename)
{
/**
* Introduce excelparser class
* The common method is
* requires path.'excelparser.php';
*/
import('@.Util.PHPExcelParser.excelparser','','.php');
$this->_excel_handle=new ExcelFileParser();
//Error getting
$this ->checkErrors($filename);
}
/**
* Error checking
*/
private function checkErrors($filename)
{
/**
* Method 1
*/
$error_code=$this->_excel_handle->ParseFromFile($filename);
/**
* 方法二
* $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 error, no processing
break;
case 1:
$this->_data=array(1,'File reading error (Linux pay attention to read and write permissions)');
break;
case 2:
$this->_data=array(1,'File too small');
break;
case 3:
$this->_data =array(1,'Failed to read Excel header');
break;
case 4:
$this->_data=array(1,'File reading error');
break;
case 5:
$this->_data=array(1,'The file may be empty');
break;
case 6:
$this-> _data=array(1,'Incomplete file');
break;
case 7:
$this->_data=array(1,'Error reading data');
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;
/**
* Get the number of sheets
* Get the rows and columns corresponding to the sheet unit
*/
$this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);
for($i=0;$i< $this->_excel['sheet_number'];$i++)
{
/**
* rows in columns
* Note: counting starts 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
*/
private function uc2html($str )
{
$ret = '';
for( $i=0; $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 mark
$this->_data[0]=1;
//Get data
for($i=0;$i<$this->_excel['sheet_number'];$i++)
{
/**
* Loop over rows
*/
for($j=0; $j<$this->_excel['row_number'][$i];$j++)
{
/**
* Alignment loop
*/
for($k=0;$k< $this->_excel['col_number'][$i];$k++)
{
/**
* array(4) {
* ["type"] => Type [0 character type 1 integer 2 floating point number 3 date]
* ["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
//TODO
break;
case 3:
//Date
//TODO
break;
}
$this->_data[1][$i][$j][$k]=$data['data' ];
unset($data);
}
}
}
}
/**
* Main function
* @return array(identifier, content s)
*/
public function main()
{
//Get Excel information
$this->getExcelInfo();
//Get Excel data
$this->getExcelData();
return $this->_data ;
}
}
?>