phpでExcelFileParserを使用してexcelを処理してデータを取得する(データベースへの一括インポートとして使用可能)

7280 ワード

 
  




Excel





Excel










 
  
/**
* CopyRight (c) 2009,
* All rights reserved.
* :
* :
*
* @author [url=mailto:[email protected]][email protected][/url]
* @version
*/

public function parse()
{
/**
* $_FILES
* array(n) {
* [" "] => array(5) {
* ["name"] =>
* ["type"] => Excel "application/vnd.ms-excel"
* ["tmp_name"] =>
* ["error"] => (0 1 upload_max_filesize2 MAX_FILE3 4 )
* ["size"] => ( :KB)
* }
* }
*/
$return=array(0,'');
/**
*
* is_uploaded_file( ) POST , , move_upload_file
*/
if(!isset($_FILES) || !is_uploaded_file($_FILES['excel']['tmp_name']))
{
$return=array(1,' ');
}
//
if(0 == $return[0])
{
import('@.Util.ExcelParser');
$excel=new ExcelParser($_FILES['excel']['tmp_name']);
$return=$excel->main();
}
//
print_r($return);
?>
 
  
/**
* CopyRight (c) 2009,
* All rights reserved.
* :excel
* :
*
* @author [url=mailto:[email protected]][email protected][/url]
* @version 0.1
*/
class ExcelParser
{
private $_data=array(0,'');
private $_excel_handle;
private $_excel=array();
/**
*
* @param $filename
*/
public function __construct($filename)
{
/**
* excelparser
*
* requires .'excelparser.php';
*/
import('@.Util.PHPExcelParser.excelparser','','.php');
$this->_excel_handle=new ExcelFileParser();
//
$this->checkErrors($filename);
}
/**
*
*/
private function checkErrors($filename)
{
/**
*
*/
$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:
//
break;
case 1:
$this->_data=array(1,' (Linux )');
break;
case 2:
$this->_data=array(1,' ');
break;
case 3:
$this->_data=array(1,' Excel ');
break;
case 4:
$this->_data=array(1,' ');
break;
case 5:
$this->_data=array(1,' ');
break;
case 6:
$this->_data=array(1,' ');
break;
case 7:
$this->_data=array(1,' ');
break;
case 8:
$this->_data=array(1,' ');
break;
}
unset($error_code);
}
/**
* Excel
*/
private function getExcelInfo()
{
if(1==$this->_data[0])return;
/**
* sheet
* sheet
*/
$this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);
for($i=0;$i_excel['sheet_number'];$i++)
{
/**
*
* : 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);
}
}
/**
*
* @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
*/
private function getExcelData()
{
if(1==$this->_data[0])return;
//
$this->_data[0]=1;
//
for($i=0;$i_excel['sheet_number'];$i++)
{
/**
*
*/
for($j=0;$j_excel['row_number'][$i];$j++)
{
/**
*
*/
for($k=0;$k_excel['col_number'][$i];$k++)
{
/**
* array(4) {
* ["type"] => [0 1 2 3 ]
* ["font"] =>
* ["data"] =>
* ...
* }
*/
$data=$this->_excel_handle->worksheet['data'][$i]['cell'][$j][$k];
switch($data['type'])
{
case 0:
//
if($this->_excel_handle->sst['unicode'][$data['data']])
{
//
$data['data'] = $this->uc2html($this->_excel_handle->sst['data'][$data['data']]);
}
else
{
$data['data'] = $this->_excel_handle->sst['data'][$data['data']];
}
break;
case 1:
//
//TODO
break;
case 2:
//
//TODO
break;
case 3:
//
//TODO
break;
}
$this->_data[1][$i][$j][$k]=$data['data'];
unset($data);
}
}
}
}
/**
*
* @return array( , s)
*/
public function main()
{
//Excel
$this->getExcelInfo();
//Excel
$this->getExcelData();
return $this->_data;
}
}
?>