CI框架(CodeIgniter)實(shí)現(xiàn)的導(dǎo)入、導(dǎo)出數(shù)據(jù)操作示例
本文實(shí)例講述了CI框架(CodeIgniter)實(shí)現(xiàn)的導(dǎo)入、導(dǎo)出數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
在libraies中引用PHPExcel這個(gè)類(phpexcel.php)
public function excel_put(){
//先做一個(gè)文件上傳,保存文件
$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文件中的第一個(gè)工作表
$currentSheet = $PHPExcel->getSheet(0);
// 取得最大的列號(hào)
$allColumn = $currentSheet->getHighestColumn();
// 取得一共有多少行
$allRow = $currentSheet->getHighestRow();
// 從第二行開始輸出,因?yàn)閑xcel表中第一行為列名
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)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Yii2 隊(duì)列 shmilyzxt/yii2-queue 簡(jiǎn)單概述
這篇文章主要介紹了Yii2 隊(duì)列 shmilyzxt/yii2-queue 的簡(jiǎn)單概述,需要的朋友可以參考下2017-08-08
PHP json_encode() 函數(shù)詳解及中文亂碼問題
這篇文章主要介紹了PHP json_encode() 函數(shù)詳解及中文亂碼問題的相關(guān)資料,需要的朋友可以參考下2015-11-11
關(guān)于PHP的相似度計(jì)算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計(jì)算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04
PHP使用Alexa API獲取網(wǎng)站的Alexa排名例子
這篇文章主要介紹了PHP使用Alexa API獲取網(wǎng)站的Alexa排名例子,需要的朋友可以參考下2014-06-06
php 問卷調(diào)查結(jié)果統(tǒng)計(jì)
一個(gè)新產(chǎn)品投入市場(chǎng),要先做問卷調(diào)查,考察這個(gè)產(chǎn)品在市場(chǎng)的需求量,本篇文章主要跟大家介紹使用php問卷調(diào)查結(jié)果統(tǒng)計(jì),感興趣的朋友一起學(xué)習(xí)吧2015-10-10
PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析
這篇文章主要介紹了PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Yii2 中實(shí)現(xiàn)單點(diǎn)登錄的方法
這篇文章主要介紹了Yii2 中實(shí)現(xiàn)單點(diǎn)登錄的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03

