PHP USES excel fileparser to process data of in excel for batch import to database

  • 2020-03-31 21:06:37
  • OfStack

 
<!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/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Excel Data acquisition demonstration </title> 
<meta name="Keywords" content="TODO" /> 
<meta name="Description" content="TODO"/> 
</head> 
<body> 
<div> 
<div>Excel Data acquisition demonstration </div> 
<div> 
<form method="POST" action="/Index/parse" enctype="multipart/form-data"> 
<input type="file" name="excel" value="" /> 
<input type="submit" name="submit" value=" submit " /> 
</form> 
</div> 
</div> 
</body> 
</html> 

 
<?php 
/** 
* CopyRight (c) 2009, 
* All rights reserved. 
*  The file name : 
*  pick   to : 
* 
* @author  Eight weeks  [url=mailto:ixqbar@hotmail.com]ixqbar@hotmail.com[/url] 
* @version 
*/ 

public function parse() 
{ 
/** 
* $_FILES An array  
* array(n) { 
* [" Form file box name "] => array(5) { 
* ["name"] =>  Name of submission  
* ["type"] =>  Submit file type  Excel for "application/vnd.ms-excel" 
* ["tmp_name"] =>  Temporary file name  
* ["error"] =>  error (0 successful 1 The file is too large to exceed upload_max_filesize2 The file is too large to exceed MAX_FILE3 Incomplete upload 4 No file uploaded ) 
* ["size"] =>  The file size ( unit :KB) 
* } 
* } 
*/ 
$return=array(0,''); 
 
if(!isset($_FILES) || !is_uploaded_file($_FILES['excel']['tmp_name'])) 
{ 
$return=array(1,' Illegal submission '); 
} 
//To deal with
if(0 == $return[0]) 
{ 
import('@.Util.ExcelParser'); 
$excel=new ExcelParser($_FILES['excel']['tmp_name']); 
$return=$excel->main(); 
} 
// The output To deal with
print_r($return); 
?> 

 
<?php 
/** 
* CopyRight (c) 2009, 
* All rights reserved. 
*  The file name :excel Data acquisition  
*  pick   to : 
* 
* @author  Eight weeks  [url=mailto:ixqbar@hotmail.com]ixqbar@hotmail.com[/url] 
* @version 0.1 
*/ 
class ExcelParser 
{ 
private $_data=array(0,''); 
private $_excel_handle; 
private $_excel=array(); 
 
public function __construct($filename) 
{ 
 
import('@.Util.PHPExcelParser.excelparser','','.php'); 
$this->_excel_handle=new ExcelFileParser(); 
//Error to get
$this->checkErrors($filename); 
} 
 
private function checkErrors($filename) 
{ 
 
$error_code=$this->_excel_handle->ParseFromFile($filename); 
 
switch($error_code) 
{ 
case 0: 
//No error not handled
break; 
case 1: 
$this->_data=array(1,' File read error (Linux Pay attention to read and write permissions )'); 
break; 
case 2: 
$this->_data=array(1,' The file is too small '); 
break; 
case 3: 
$this->_data=array(1,' read Excel Header failure '); 
break; 
case 4: 
$this->_data=array(1,' File read 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,' Reading error '); 
break; 
case 8: 
$this->_data=array(1,' Version of the error '); 
break; 
} 
unset($error_code); 
} 
 
private function getExcelInfo() 
{ 
if(1==$this->_data[0])return; 
 
$this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']); 
for($i=0;$i<$this->_excel['sheet_number'];$i++) 
{ 
 
$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); 
} 
} 
 
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'); 
} 
 
private function getExcelData() 
{ 
if(1==$this->_data[0])return; 
//Change token
$this->_data[0]=1; 
//To get the data
for($i=0;$i<$this->_excel['sheet_number'];$i++) 
{ 
 
for($j=0;$j<$this->_excel['row_number'][$i];$j++) 
{ 
 
for($k=0;$k<$this->_excel['col_number'][$i];$k++) 
{ 
 
$data=$this->_excel_handle->worksheet['data'][$i]['cell'][$j][$k]; 
switch($data['type']) 
{ 
case 0: 
//Character types
if($this->_excel_handle->sst['unicode'][$data['data']]) 
{ 
//Chinese language 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: 
//The integer
//TODO 
break; 
case 2: 
//Floating point Numbers
//TODO 
break; 
case 3: 
//The date of
//TODO 
break; 
} 
$this->_data[1][$i][$j][$k]=$data['data']; 
unset($data); 
} 
} 
} 
} 
 
public function main() 
{ 
//Excel information acquisition
$this->getExcelInfo(); 
//Excel data acquisition
$this->getExcelData(); 
return $this->_data; 
} 
} 
?> 

Related articles: