当前位置:Gxlcms > PHP教程 > excel的导入导出

excel的导入导出

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

导入

  1. public function excel_put(){
  2. //先做一个文件上传,保存文件
  3. $path=$_FILES['file'];
  4. $filePath = "uploads/".$path["name"];
  5. move_uploaded_file($path["tmp_name"],$filePath);
  6. //默认用excel2007读取excel,若格式不对,则用之前的版本进行读取
  7. //表格字段名字
  8. $data=array('B'=>'name','C'=>'pwd','D'=>'money1','E'=>'salt');
  9. $tablename='user1';//表名字
  10. $this->excel_fileput($filePath,$data,$tablename);
  11. }
  12. private function excel_fileput($filePath,$data,$tablename){
  13. $this->load->library("phpexcel");//ci框架中引入excel类
  14. $PHPExcel = new PHPExcel();
  15. $PHPReader = new PHPExcel_Reader_Excel2007();
  16. if(!$PHPReader->canRead($filePath)){
  17. $PHPReader = new PHPExcel_Reader_Excel5();
  18. if(!$PHPReader->canRead($filePath)){
  19. echo 'no Excel';
  20. return ;
  21. }
  22. }
  23. // 加载excel文件
  24. $PHPExcel = $PHPReader->load($filePath);
  25. // 读取excel文件中的第一个工作表
  26. $currentSheet = $PHPExcel->getSheet(0);
  27. // 取得最大的列号
  28. $allColumn = $currentSheet->getHighestColumn();
  29. // 取得一共有多少行
  30. $allRow = $currentSheet->getHighestRow();
  31. // 从第二行开始
输出,因为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 "
"; } //echo "\n"; echo "导入成功"; }
导出

  1. header("Content-type:application/vnd.ms-excel");
  2. header("Content-Disposition:attachment;filename=123.xls");
  3. $array=$this->db->get("shop_address")->result_array();
  4. $str = "Id\tName\tPid\n";
  5. foreach ($array as $val) {
  6. $str .= $val['id'] . "\t" .$val['name'] . "\t" . $val['pid'] . "\n";
  7. }
  8. echo $str;

以上就介绍了 excel的导入导出,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行