欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php中使用ExcelFileParser處理excel獲得數(shù)據(jù)(可作批量導(dǎo)入到數(shù)據(jù)庫(kù)使用)

 更新時(shí)間:2010年08月21日 17:53:22   作者:  
使用ExcelFileParser處理excel獲得數(shù)據(jù) 可以用作批量導(dǎo)入到數(shù)據(jù)庫(kù)使用,需要獲取excel數(shù)據(jù)的朋友可以參考下。
復(fù)制代碼 代碼如下:

<!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="text/html; charset=utf-8" />
<title>Excel數(shù)據(jù)獲取演示</title>
<meta name="Keywords" content="TODO" />
<meta name="Description" content="TODO"/>
</head>
<body>
<div>
<div>Excel數(shù)據(jù)獲取演示</div>
<div>
<form method="POST" action="/Index/parse" enctype="multipart/form-data">
<input type="file" name="excel" value="" />
<input type="submit" name="submit" value="提交" />
</form>
</div>
</div>
</body>
</html>

復(fù)制代碼 代碼如下:

<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
* 文件名:
* 摘 要:
*
* @author 星期八 [url=mailto:ixqbar@hotmail.com]ixqbar@hotmail.com[/url]
* @version
*/

public function parse()
{
/**
* $_FILES數(shù)組說(shuō)明
* array(n) {
* ["表單文件框名稱"] => array(5) {
* ["name"] => 提交文件名稱
* ["type"] => 提交文件類型 Excel為"application/vnd.ms-excel"
* ["tmp_name"] => 臨時(shí)文件名稱
* ["error"] => 錯(cuò)誤(0成功1文件太大超過(guò)upload_max_filesize2文件太大超過(guò)MAX_FILE3上傳不完整4沒(méi)有上傳文件)
* ["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);
?>

復(fù)制代碼 代碼如下:

<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
* 文件名:excel數(shù)據(jù)獲取
* 摘 要:
*
* @author 星期八 [url=mailto:ixqbar@hotmail.com]ixqbar@hotmail.com[/url]
* @version 0.1
*/
class ExcelParser
{
private $_data=array(0,'');
private $_excel_handle;
private $_excel=array();
/**
* 構(gòu)造函數(shù)
* @param <string> $filename 上傳文件臨時(shí)文件名稱
*/
public function __construct($filename)
{
/**
* 引入excelparser類
* 普通方法為
* requires 路徑.'excelparser.php';
*/
import('@.Util.PHPExcelParser.excelparser','','.php');
$this->_excel_handle=new ExcelFileParser();
//錯(cuò)誤獲取
$this->checkErrors($filename);
}
/**
* 錯(cuò)誤校驗(yàn)
*/
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:
//無(wú)錯(cuò)誤不處理
break;
case 1:
$this->_data=array(1,'文件讀取錯(cuò)誤(Linux注意讀寫權(quán)限)');
break;
case 2:
$this->_data=array(1,'文件太小');
break;
case 3:
$this->_data=array(1,'讀取Excel表頭失敗');
break;
case 4:
$this->_data=array(1,'文件讀取錯(cuò)誤');
break;
case 5:
$this->_data=array(1,'文件可能為空');
break;
case 6:
$this->_data=array(1,'文件不完整');
break;
case 7:
$this->_data=array(1,'讀取數(shù)據(jù)錯(cuò)誤');
break;
case 8:
$this->_data=array(1,'版本錯(cuò)誤');
break;
}
unset($error_code);
}
/**
* Excel信息獲取
*/
private function getExcelInfo()
{
if(1==$this->_data[0])return;
/**
* 獲得sheet數(shù)量
* 獲得sheet單元對(duì)應(yīng)的行和列
*/
$this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);
for($i=0;$i<$this->_excel['sheet_number'];$i++)
{
/**
* 行于列
* 注意:從0開(kāi)始計(jì)數(shù)
*/
$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);
}
}
/**
* 中文處理函數(shù)
* @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數(shù)據(jù)獲取
*/
private function getExcelData()
{
if(1==$this->_data[0])return;
//修改標(biāo)記
$this->_data[0]=1;
//獲取數(shù)據(jù)
for($i=0;$i<$this->_excel['sheet_number'];$i++)
{
/**
* 對(duì)行循環(huán)
*/
for($j=0;$j<$this->_excel['row_number'][$i];$j++)
{
/**
* 對(duì)列循環(huán)
*/
for($k=0;$k<$this->_excel['col_number'][$i];$k++)
{
/**
* array(4) {
* ["type"] => 類型 [0字符類型1整數(shù)2浮點(diǎn)數(shù)3日期]
* ["font"] => 字體
* ["data"] => 數(shù)據(jù)
* ...
* }
*/
$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:
//整數(shù)
//TODO
break;
case 2:
//浮點(diǎn)數(shù)
//TODO
break;
case 3:
//日期
//TODO
break;
}
$this->_data[1][$i][$j][$k]=$data['data'];
unset($data);
}
}
}
}
/**
* 主函數(shù)
* @return <array> array(標(biāo)識(shí)符,內(nèi)容s)
*/
public function main()
{
//Excel信息獲取
$this->getExcelInfo();
//Excel數(shù)據(jù)獲取
$this->getExcelData();
return $this->_data;
}
}
?>

相關(guān)文章

  • php 無(wú)法載入mysql擴(kuò)展

    php 無(wú)法載入mysql擴(kuò)展

    無(wú)法載入 mysql 擴(kuò)展的實(shí)現(xiàn)代碼。
    2010-03-03
  • Can''t create/write to file ''C:\WINDOWS\TEMP\...MYSQL報(bào)錯(cuò)解決方法

    Can''t create/write to file ''C:\WINDOWS\TEMP\...MYSQL報(bào)錯(cuò)解決方法

    Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL報(bào)錯(cuò)解決方法,參考下面的方法即可。
    2011-06-06
  • php基于curl重寫file_get_contents函數(shù)實(shí)例

    php基于curl重寫file_get_contents函數(shù)實(shí)例

    這篇文章主要介紹了php基于curl重寫file_get_contents函數(shù)的方法,結(jié)合實(shí)例形式分析了php使用curl重寫file_get_contents函數(shù)實(shí)現(xiàn)屏蔽錯(cuò)誤提示的相關(guān)技巧,需要的朋友可以參考下
    2016-11-11
  • PHP排序算法系列之歸并排序詳解

    PHP排序算法系列之歸并排序詳解

    這篇文章主要為大家詳細(xì)介紹了PHP排序算法系列之歸并排序的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • php郵件發(fā)送功能實(shí)現(xiàn)詳解

    php郵件發(fā)送功能實(shí)現(xiàn)詳解

    隨著企業(yè)化的管理越來(lái)越規(guī)范,各種項(xiàng)目管理系統(tǒng)中,都需要加入到郵件實(shí)時(shí)通知功能,所以在項(xiàng)目中如何整合發(fā)郵件功能,其實(shí)也是很重要的一點(diǎn)。本文為大家介紹了PHP實(shí)現(xiàn)郵件實(shí)時(shí)通知功能的示例代碼,需要的可以參考一下
    2022-09-09
  • PHP中redis的用法深入解析

    PHP中redis的用法深入解析

    本篇文章主要是對(duì)PHP中redis的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2014-02-02
  • 淺談定義一個(gè)PHP函數(shù)

    淺談定義一個(gè)PHP函數(shù)

    在過(guò)去很長(zhǎng)一段時(shí)間里,PHP都是開(kāi)發(fā)web應(yīng)用的不二之選?,F(xiàn)在7.x版本又填補(bǔ)了許多高級(jí)特性和現(xiàn)代化應(yīng)用的需求,并且提高了開(kāi)發(fā)者的效率。這門語(yǔ)言正不斷的發(fā)生改變,找出這些變化,并停止過(guò)去的寫法,放棄你原來(lái)的習(xí)慣并自豪的使用這些新特性,讓你的代碼更易讀易懂。
    2021-05-05
  • php微信支付之APP支付方法

    php微信支付之APP支付方法

    這篇文章主要介紹了php微信支付之APP支付方法,實(shí)例分析了php微信支付接口文件及使用技巧,需要的朋友可以參考下
    2015-03-03
  • php中配置文件操作 如config.php文件的讀取修改等操作

    php中配置文件操作 如config.php文件的讀取修改等操作

    對(duì)形如config.php文件的讀取,修改等操作的代碼,需要的朋友可以參考下
    2012-07-07
  • php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例

    php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例

    這篇文章主要介紹了php+pdo實(shí)現(xiàn)的購(gòu)物車類,結(jié)合完整實(shí)例形式分析了PHP結(jié)合pdo操作數(shù)據(jù)庫(kù)讀寫實(shí)現(xiàn)購(gòu)物車功能相關(guān)實(shí)現(xiàn)與使用方法,需要的朋友可以參考下
    2020-01-01

最新評(píng)論