


Use ExcelFileParser in PHP to process excel to obtain data (can be imported into the database in batches)_PHP tutorial
< ;/html>
/ **
* 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);
?>
/**
* 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
*/
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
*/
public function main()
{
//Get Excel information
$this->getExcelInfo();
//Get Excel data
$this->getExcelData();
return $this->_data ;
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
