Copy code The code is as follows:
Excel data acquisition demonstration< ;/title>
Excel data acquisition demonstration
< ;/html>
Copy code The code is as follows:
/ **
* CopyRight (c) 2009,
* All rights reserved.
* File name:
* Summary:
*
* @author Saturday [url=mailto:ixqbar@ hotmail.com]ixqbar@hotmail.com[/url]
* @version
*/
public function parse()
{
/**
* $_FILES array description
* array(n) {
* ["form file box name"] => array(5) {
* ["name"] => Submit File name
* ["type"] => Submit file type Excel as "application/vnd.ms-excel"
* ["tmp_name"] => Temporary file name
* ["error "] => Error (0 Success 1 File too large exceeds upload_max_filesize 2 File too large exceeds MAX_FILE 3 Incomplete upload 4 No file uploaded)
* ["size"] => File size (unit: KB)
* }
* }
*/
$return=array(0,'');
/**
* Determine whether to submit
* is_uploaded_file (file name) is used to determine whether the specified file is uploaded using the POST method to prevent illegal submission. It is usually used together with move_upload_file to save the uploaded file to the specified path
*/
if(!isset($_FILES) || !is_uploaded_file($_FILES['excel']['tmp_name']))
{
$return=array( 1,'Submission is illegal');
}
//Processing
if(0 == $return[0])
{
import('@.Util.ExcelParser') ;
$excel=new ExcelParser($_FILES['excel']['tmp_name']);
$return=$excel->main();
}
//Output processing
print_r($return);
?>
Copy code The code is as follows:
/**
* 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 ;
}
}
?>
http://www.bkjia.com/PHPjc/322271.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322271.htmlTechArticleCopy the code as follows: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/199...