Excelfileparser processing Excel to get data for bulk import into a database _php tutorial

Source: Internet
Author: User
Tags ord
Excelfileparser processing Excel to get data for bulk import into the database

Submit Form





Excel Data Capture Demo





Excel Data Capture Demo






Submit Processing
[PHP]
/**
* CopyRight (c) 2009,
* All rights reserved.
FileName
Summary
*
* @author Week eight ixqbar@hotmail.com
* @version
*/

Class Indexaction extends Action
{
/**
* Constructor function
*/
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 for "Application/vnd.ms-excel"
* ["tmp_name"] + = Temp file name
* ["error"] = + Error (0 Success 1 file too large more than Upload_max_filesize2 file is too large for max_file3 upload incomplete 4 not uploaded file)
* ["size"] + = File size (in 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, prevents illegal submissions, and is usually used 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 not lawful ');
}
Processing
if (0 = = $return [0])
{
Import (' @. Util.excelparser ');
$excel =new excelparser ($_files[' Excel ' [' Tmp_name ']);
$return = $excel->main ();
}
Output processing
Print_r ($return);
}
}
?>
[/php]

Processing class
[PHP]
/**
* CopyRight (c) 2009,
* All rights reserved.
* File name: Excel data acquisition
Summary
*
* @author Week eight ixqbar@hotmail.com
* @version 0.1
*/
Class Excelparser
{
Private $_data=array (0, ');
Private $_excel_handle;
Private $_excel=array ();
/**
* Constructor function
* @param $filename Upload File Temp file name
*/
Public function __construct ($filename)
{
/**
* Introduction of Excelparser Class
* Common method is
* Requires path. ' Excelparser.php ';
* Import for thinkphp with imported class method
*/
Import (' @. Util.PHPExcelParser.excelparser ', ', '. php ');
$this->_excel_handle=new Excelfileparser ();
Error fetching
$this->checkerrors ($filename);
}
/**
* Error Checking
*/
Private Function Checkerrors ($filename)
{
/**
* Method One
*/
$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 errors not handled
Break
Case 1:
$this->_data=array (1, ' File read error (Linux note read and write access) ');
Break
Case 2:
$this->_data=array (1, ' File is too small ');
Break
Case 3:
$this->_data=array (1, ' failed to read Excel header ');
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;
/**
* Number of sheet received
* 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: Counting 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
*/
Private Function uc2html ($STR)
{
$ret = ";
for ($i =0; $i
{
$charcode = Ord ($str [$i]) +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 Tags
$this->_data[0]=1;
Get Data
for ($i =0; $i < $this->_excel[' Sheet_number '); $i + +)
{
/**
* Loop on line
*/
for ($j =0; $j < $this->_excel[' row_number '] [$i]; $j + +)
{
/**
* Cycle to Columns
*/
for ($k =0; $k < $this->_excel[' Col_number '] [$i]; $k + +)
{
/**
* Array (4) {
* ["type"] = = Type [0 character type 1 integer 2 floating point 3rd]
* ["font"] + = Font
* ["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 number
Todo
Break
Case 3:
Date
Todo
Break
}
$this->_data[1][$i] [$j] [$k]= $data [' Data '];
Unset ($data);
}
}
}
}
/**
* Main function
* @return Array (identifier, content s)
*/
Public Function Main ()
{
Excel Information Acquisition
$this->getexcelinfo ();
Excel Data acquisition
$this->getexceldata ();
return $this->_data;
}
}

?>


http://www.bkjia.com/PHPjc/444996.html www.bkjia.com true http://www.bkjia.com/PHPjc/444996.html techarticle Excelfileparser processing Excel to get data can be bulk imported into the database submission form! DOCTYPE HTML Public-//w3c//dtd XHTML 1.0 transitional//en http://www.w3.org/TR/xhtml1/DTD/xhtml ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.