Last Update:2017-09-14
Source: Internet
Author: User
Keywords
Web Programming
PHP Tutorials
We are using this extension of excelfileparser to handle this, oh,
<! 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= "HTML; Charset=utf-8 "/> <title>excel Data capture demo </title> <meta name=" Keywords "content=" TODO "/> <meta name= "Description" content= "TODO"/> </head> <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= "Submission" name= "submit" value= "submitted"/> </form> </div> </DIV> </body> </html>
<?php /** * CopyRight (c) 2009, * All rights reserved. File: Summary: * * @author Week eight [Email=ixqbar@hotmail.com]ixqbar@hotmail.com[/email] * @version */
class Indexaction extends Action { /** * Constructor */ Public Function __construct () { parent::__construct (); } /** * Default index page */ Public Function index () { $this->display (); } /** * Submit Processing */ 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 successful 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 typically uses the Save upload file with Move_upload_file to the specified path */ if (!isset ($_files) | |!is_uploaded_file ($_files[' Excel '] [' tmp_name ']) { $return =array (1, ' submit illegal '); } //Handling if (0 = = $return [0]) { import (' @. Util.excelparser '); $excel =new excelparser ($_files[' Excel '] [' tmp_name ']); $return = $excel->main (); } //Output processing Print_r ($return); } } ?>
<?php /** * CopyRight (c) 2009, * All rights reserved. * FileName: Excel data acquisition * Abstract: * * @author Week eight [email=ixqbar@hotmail.com]ixqbar@hotmail.com[/email] * @version 0.1 */ class Excelparser { Private $_data=array (0, ""); private $_excel_handle; private $_excel=array (); /** * Constructor * @param <string> $filename upload file temporary file name */ Public Function __construct ($filename) { /** * introduced Excelparser class * Common method for * requires path. ' Excelparser.php '; * Import is a thinkphp with imported class method */ Import (' @. Util.PHPExcelParser.excelparser ', ', '. php '); $this->_excel_handle=new excelfileparser (); //Error getting $this->checkerrors ($filename); } /** * Error Checking */ Private Function Checkerrors ($filename) { /** * Method A */ $error _code= $this->_excel_handle->parsefromfile ($filename); /** * Method Two * $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 not handling 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 Access */ Private Function Getexcelinfo () { if (1== $this->_data[0]) return; /** * Access to sheet quantity * Gets the row and column corresponding to the sheet cell */ $this->_excel[' Sheet_number ']=count ($this->_excel_handle->worksheet[' name ')); for ($i =0 $i < $this->_excel[' sheet_number '; $i + +) { /** * row in 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 Tag $this->_data[0]=1; //Get Data for ($i =0 $i < $this->_excel[' sheet_number '; $i + +) { /** * On-line cycling */ for ($j =0; $j < $this->_excel[' row_number '] [$i]; $j + +) { /** * the column loop */ 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 //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; } }
?>