当前位置:Gxlcms > PHP教程 > CI框架(CodeIgniter)实现的导入、导出数据操作

CI框架(CodeIgniter)实现的导入、导出数据操作

时间:2021-07-01 10:21:17 帮助过:32人阅读

这篇文章主要介绍了CI框架(CodeIgniter)实现的导入、导出数据操作,结合实例形式分析了CodeIgniter框架libraies中引用PHPExcel实现数据导入导出相关操作技巧,需要的朋友可以参考下

本文实例讲述了CI框架(CodeIgniter)实现的导入、导出数据操作。分享给大家供大家参考,具体如下:

在libraies中引用PHPExcel这个类(phpexcel.php)

  1. public function excel_put(){
  2. //先做一个文件上传,保存文件
  3. $path=$_FILES['file'];
  4. $filePath = "uploads/".$path["name"];
  5. move_uploaded_file($path["tmp_name"],$filePath);
  6. //$data=array('B'=>'name','C'=>'pwd','D'=>'money1','E'=>'salt');
  7. $data=array('B'=>'name','C'=>'pid');
  8. $tablename='city2';//表名字
  9. $this->excel_fileput($filePath,$data,$tablename);
  10. }

  1. private function excel_fileput($filePath,$data,$tablename){
  2. $this->load->library("phpexcel");//ci框架中引入excel类
  3. $PHPExcel = new PHPExcel();
  4. $PHPReader = new PHPExcel_Reader_Excel2007();
  5. if(!$PHPReader->canRead($filePath)){
  6. $PHPReader = new PHPExcel_Reader_Excel5();
  7. if(!$PHPReader->canRead($filePath)){
  8. echo 'no Excel';
  9. return ;
  10. }
  11. }
  12. // 加载excel文件
  13. $PHPExcel = $PHPReader->load($filePath);
  14. // 读取excel文件中的第一个工作表
  15. $currentSheet = $PHPExcel->getSheet(0);
  16. // 取得最大的列号
  17. $allColumn = $currentSheet->getHighestColumn();
  18. // 取得一共有多少行
  19. $allRow = $currentSheet->getHighestRow();
  20. // 从第二行开始
输出,因为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 "导入成功"; }

导出数据:

  1. public function excel_out(){
  2. header("Content-type:text/html");
  3. header("Content-Disposition:attachment;filename=123.xls");
  4. $array=$this->db->get("city")->result_array();
  5. $str="id\t"."name\t"."pid\n";
  6. foreach($array as $val){
  7. $str.=$val['id']."\t".$val['name']."\t".$val['pid']."\n";
  8. }
  9. echo $str;
  10. }

以上就是本篇文章的全部内容了,感谢大家阅读。更多请关注PHP中文网!

相关推荐:

CodeIgniter框架数据库基本操作示例

以上就是CI框架(CodeIgniter)实现的导入、导出数据操作的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行