Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>excel Data Capture Demo </title>
<meta name= "Keywords" content= "TODO"/>
<meta name= "Description" content= "TODO"/>
<body>
<div>
<div>excel Data Capture Demo </div>
<div>
<form method= "POST" action= "/index/parse" enctype= "Multipart/form-data" >
<input type= "file" Name= "Excel" Value= ""/>
<input type= "Submit" name= "Submission" value= "submitted"/>
</form>
</div>
</div>
</body>
Copy Code code as follows:
<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
FileName
Summary
*
* @author Week eight [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 '] => commit file type Excel to "Application/vnd.ms-excel"
* ["Tmp_name"] => temporary file name
* ["Error"] => error (0 success 1 files too large over upload_max_filesize2 file too large over max_file3 upload incomplete 4 no upload file)
* [' size '] => file size (in KB)
* }
* }
*/
$return =array (0, "");
/**
* Judge whether to submit
* Is_uploaded_file (file name) is used to determine whether the specified file is uploaded using the Post method, prevents illegal submission, and is typically used with Move_upload_file to save uploaded files to the specified path
*/
if (!isset ($_files) | | |!is_uploaded_file ($_files[' Excel '] [' tmp_name '])
{
$return =array (1, ' submit 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 code as follows:
<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
* File name: Excel data acquisition
Summary
*
* @author Week eight [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 <string> $filename file Temporary file name
*/
Public function __construct ($filename)
{
/**
* Introduction of Excelparser Class
* Common method is
* Requires path. ' Excelparser.php ';
*/
Import (' @. Util.PHPExcelParser.excelparser ', ', '. php ');
$this->_excel_handle=new Excelfileparser ();
Error getting
$this->checkerrors ($filename);
}
/**
* ERROR Check
*/
Private Function Checkerrors ($filename)
{
/**
* Method One
*/
$error _code= $this->_excel_handle->parsefromfile ($filename);
/**
* Method II
* $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 errors not handled
Break
Case 1:
$this->_data=array (1, ' File read error (Linux note read-write permission) ');
Break
Case 2:
$this->_data=array (1, ' file too small ');
Break
Case 3:
$this->_data=array (1, ' Read Excel header failed ');
Break
Case 4:
$this->_data=array (1, ' file read error ');
Break
Case 5:
$this->_data=array (1, ' file may be empty ');
Break
Case 6:
$this->_data=array (1, ' incomplete file ');
Break
Case 7:
$this->_data=array (1, ' Read data error ');
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;
/**
* Access to Sheet quantity
* Get the row and column corresponding to the sheet unit
*/
$this->_excel[' Sheet_number ']=count ($this->_excel_handle->worksheet[' name '));
For ($i =0 $i < $this->_excel[' Sheet_number '); $i + +)
{
/**
* Row to Column
* Note: Count starting 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 <string>
*/
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 ');
}
/**
* Excel Data acquisition
*/
Private Function Getexceldata ()
{
if (1== $this->_data[0]) return;
Modify Markup
$this->_data[0]=1;
Get Data
For ($i =0 $i < $this->_excel[' Sheet_number '); $i + +)
{
/**
* Loop to Line
*/
for ($j =0; $j < $this->_excel[' row_number '] [$i]; $j + +)
{
/**
* Loop to Column
*/
for ($k =0; $k < $this->_excel[' Col_number '] [$i]; $k + +)
{
/**
* Array (4) {
* [' type '] => type [0 character type 1 integer 2 floating-point number 3rd]
* ["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 numbers
Todo
Break
Case 3:
Date
Todo
Break
}
$this->_data[1][$i] [$j] [$k]= $data [' Data '];
Unset ($data);
}
}
}
}
/**
* Main function
* @return <array> Array (identifier, content s)
*/
Public Function Main ()
{
Excel Information Acquisition
$this->getexcelinfo ();
Excel Data acquisition
$this->getexceldata ();
return $this->_data;
}
}
?>