CI框架(CodeIgniter)實現(xiàn)的導(dǎo)入、導(dǎo)出數(shù)據(jù)操作示例
本文實例講述了CI框架(CodeIgniter)實現(xiàn)的導(dǎo)入、導(dǎo)出數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
在libraies中引用PHPExcel這個類(phpexcel.php)
public function excel_put(){ //先做一個文件上傳,保存文件 $path=$_FILES['file']; $filePath = "uploads/".$path["name"]; move_uploaded_file($path["tmp_name"],$filePath); //$data=array('B'=>'name','C'=>'pwd','D'=>'money1','E'=>'salt'); $data=array('B'=>'name','C'=>'pid'); $tablename='city2';//表名字 $this->excel_fileput($filePath,$data,$tablename); }
private function excel_fileput($filePath,$data,$tablename){ $this->load->library("phpexcel");//ci框架中引入excel類 $PHPExcel = new PHPExcel(); $PHPReader = new PHPExcel_Reader_Excel2007(); if(!$PHPReader->canRead($filePath)){ $PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($filePath)){ echo 'no Excel'; return ; } } // 加載excel文件 $PHPExcel = $PHPReader->load($filePath); // 讀取excel文件中的第一個工作表 $currentSheet = $PHPExcel->getSheet(0); // 取得最大的列號 $allColumn = $currentSheet->getHighestColumn(); // 取得一共有多少行 $allRow = $currentSheet->getHighestRow(); // 從第二行開始輸出,因為excel表中第一行為列名 for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ /**從第A列開始輸出*/ //echo $allColumn; for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){ $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue(); //print_r($val); //die; if($currentColumn == 'A') { //echo $val."\t"; }else if($currentColumn <= $allColumn){ $data1[$currentColumn]=$val; } } foreach($data as $key=>$val){ $data2[$val]=$data1[$key]; } $this->db->insert($tablename,$data2); //print_r($data2); //echo "</br>"; } //echo "\n"; echo "導(dǎo)入成功"; }
導(dǎo)出數(shù)據(jù):
public function excel_out(){ header("Content-type:text/html"); header("Content-Disposition:attachment;filename=123.xls"); $array=$this->db->get("city")->result_array(); $str="id\t"."name\t"."pid\n"; foreach($array as $val){ $str.=$val['id']."\t".$val['name']."\t".$val['pid']."\n"; } echo $str; }
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
Yii2 隊列 shmilyzxt/yii2-queue 簡單概述
這篇文章主要介紹了Yii2 隊列 shmilyzxt/yii2-queue 的簡單概述,需要的朋友可以參考下2017-08-08PHP json_encode() 函數(shù)詳解及中文亂碼問題
這篇文章主要介紹了PHP json_encode() 函數(shù)詳解及中文亂碼問題的相關(guān)資料,需要的朋友可以參考下2015-11-11關(guān)于PHP的相似度計算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04PHP使用Alexa API獲取網(wǎng)站的Alexa排名例子
這篇文章主要介紹了PHP使用Alexa API獲取網(wǎng)站的Alexa排名例子,需要的朋友可以參考下2014-06-06PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析
這篇文章主要介紹了PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07