本文实例讲述了ci框架(codeigniter)实现的导入、导出数据操作。分享给大家供大家参考,具体如下:
在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 "导入成功";
}
导出数据:
public function excel_out(){
header("content-type:text/html");
header("content-disposition:attachment;filename=123.xls");
$array=$this->db->get("city")->result_array();
$str="idt"."namet"."pidn";
foreach($array as $val){
$str.=$val['id']."t".$val['name']."t".$val['pid']."n";
}
echo $str;
}
更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于codeigniter框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!