一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

ThinkPHP與PHPExcel沖突解決方法

很早之前就知道有一個叫做phpExcel的類(官方網(wǎng)站)可以用來操作Excel,一直沒有機會嘗試,今天試用發(fā)現(xiàn)無比強大,下載后的源碼包里有詳細(xì)文檔,幾乎能實現(xiàn)手工操作Excel能實現(xiàn)的一切功能。
一個簡單的讀取Excel的例子如下:
復(fù)制代碼 代碼如下:
$inputFileType = 'Excel2007';
$inputFileName = './public/files/import_user_template.xlsx';
$sheetname = 'Sheet1';
//指定Excel類型,創(chuàng)建一個reader
$objReader = phpExcel_IOFactory::createReader($inputFileType);
//設(shè)置只讀取數(shù)據(jù),不包括公式和格式
$objReader->setReadDataOnly(true);
//只讀取指定的sheet
$objReader->setLoadSheetsOnly($sheetname);
$objphpExcel = $objReader->load($inputFileName);
$curSheet = $objphpExcel->getSheet(0);
//包含數(shù)據(jù)的最大列
$allColumn = $curSheet->getHighestColumn();
//包含數(shù)據(jù)的最大行
$allRow = $curSheet->getHighestRow();
for($currentRow = 1; $currentRow <= $allRow; $currentRow++){
for($currentCol = 'A'; $currentCol <= $allColumn; $currentCol++){
echo $curSheet->getCell($currentCol.$currentRow)->getValue()."/t";
}
echo "/r/n";
}

要在Thinkphp中使用,把源碼包中的Classes目錄復(fù)制到Thinkphp的Vendor目錄下,改名為phpExcel,然后調(diào)用Vendor方法載入
復(fù)制代碼 代碼如下:
vendor('phpExcel.phpExcel');

可是這樣一來發(fā)現(xiàn)讀取Excel以后再調(diào)用M或者D方法實例化模型類時報找不到Model類的錯誤,經(jīng)過研究發(fā)現(xiàn)是自動裝載機制沖突,要解決沖突,需要在M或者D方法調(diào)用之前使用spl_autoload_register函數(shù)重新注冊autoloader類
復(fù)制代碼 代碼如下:
spl_autoload_register(array('Think','autoload'));

在Thinkphp中調(diào)用phpExcel的問題解決方案
在Thinkphp中調(diào)用phpExcel時,數(shù)據(jù)可以完全讀出來,但是下一步D,M或調(diào)用模板的時候會出錯。(不知道是我一個人遇到這個問題 嗎?)
經(jīng)過研究,終于找到了解決方法。和大家分享一下。呵呵!
1,首先下載phpExcel的包,放在 Thinkphp/Vendor/(也就是Think的第三方類庫目錄)下。
2,調(diào)用函數(shù)。
復(fù)制代碼 代碼如下:
protected function Import_Execl($file){
if(!file_exists($file)){
return array("error"=>1);
}
Vendor("phpExcel.phpExcel");
$phpExcel = new phpExcel();
$phpReader = new phpExcel_Reader_Excel2007();
if(!$phpReader->canRead($file)){
$phpReader = new phpExcel_Reader_Excel5();
if(!$phpReader->canRead($file)){
return array("error"=>2);
}
}
$phpExcel = $phpReader->load($file);
$SheetCount = $phpExcel->getSheetCount();
for($i=0;$i<$SheetCount;$i++){
$currentSheet = $phpExcel->getSheet($i);
$allColumn = $this->ExcelChange($currentSheet->getHighestColumn());
$allRow = $currentSheet->getHighestRow();
$array[$i]["Title"] = $currentSheet->getTitle();
$array[$i]["Cols"] = $allColumn;
$array[$i]["Rows"] = $allRow;
$arr = array();
for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){
$row = array();
for($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){
$row[$currentColumn] = $currentSheet->getCellByColumnAndRow($currentColumn,$currentRow)->getValue();
}
$arr[$currentRow] = $row;
}
$array[$i]["Content"] = $arr;
}
spl_autoload_register(array('Think','autoload'));//必須的,不然ThinkphpphpExcel會沖突
unset($currentSheet);
unset($phpReader);
unset($phpExcel);
unlink($file);
return array("error"=>0,"data"=>$array);
}
protected function ExcelChange($str){//配合Execl批量導(dǎo)入的函數(shù)
$len = strlen($str)-1;
$num = 0;
for($i=$len;$i>=0;$i--){
$num += (ord($str[$i]) - 64)*pow(26,$len-$i);
}
return $num;
}

3,調(diào)用。
復(fù)制代碼 代碼如下:
public function import(){
if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)){
$result = $this->Import_Execl($_FILES["import"]["tmp_name"]);
if($this->Execl_Error[$result["error"]] == 0){
$execl_data = $result["data"][0]["Content"];
unset($execl_data[1]);
$data = D("Data");
foreach($execl_data as $k=>$v){
$d["serial_no"] = $v[0];
$d["check_no"] = $v[1];
$d["work_no"] = $v[2];
$d["class_name"] = $v[3];
$d["user_name"] = $v[4];
$d["new_class"] = $v[5];
$d["error_level"] = $v[6];
$data->data($d)->add();
}
$this->success($this->Execl_Error[$result["error"]]);
}else{
$this->error($this->Execl_Error[$result["error"]]);
}
}else{
$this->error("上傳文件失敗");
}
}

4,錯誤數(shù)據(jù):
復(fù)制代碼 代碼如下:
protected $Execl_Error = array("數(shù)據(jù)導(dǎo)入成功","找不到文件","Execl文件格式不正確");

php技術(shù)ThinkPHP與PHPExcel沖突解決方法,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久久不卡国产精品一区二区 | 六月婷婷在线观看 | 亚洲成a人片777777网站 | 成年人国产视频 | 国产伦精品一区二区三区女 | 五月婷婷在线免费观看 | 91在线视频免费播放 | 亚洲人成小说 | 美女毛片在线 | 九九精彩视频在线观看视频 | 中文字幕一区二区在线观看 | 中文字幕视频网 | 夜福利视频 | 在线欧美色 | 性欧美videosg最新另类 | 中文字幕日本一本二本三区 | 久久国内视频 | 大伊人青草狠狠久久 | 国产亚洲精品国产福利在线观看 | 国产成人久久蜜一区二区 | 亚洲 欧美 校园 | 亚洲美女视频一区 | 黄色网页在线观看 | 国产床戏无遮挡免费观看网站 | 国产91精品高清一区二区三区 | 国产情侣真实露脸在线最新 | 久久久久久久久一级毛片 | 亚洲高清二区 | 国产精品久久婷婷六月丁香 | 夜夜穞狠狠穞 | 国产精品福利午夜在线观看 | 亚洲免费色视频 | 成人免费观看www视频 | 国产精品第六页 | 色在线视频免费 | 激情丁香小说 | 91精品视频网 | 99情趣网 | 激情综合文学 | 99热在线只有精品 | 色老板视频在线 |