澳门威利斯人_威利斯人娱乐「手机版」

来自 威利斯人娱乐 2019-07-06 17:46 的文章
当前位置: 澳门威利斯人 > 威利斯人娱乐 > 正文

用表格输出excel,thinkPHP导出csv文件及用表格输出

1.thinkphp导出csv文件

本文实例汇报了thinkPHP导出csv文件及用表格输出excel的艺术。共享给大家供大家参照他事他说加以考察,具体如下:

导出csv文件恐怕就那几行代码,今日有个难点找麻烦自个儿好久,正是导出之后出现有的html代码,那个不该,view里面是空的,controller中最终也未有$this->display(),最终留神看到think_page_trace那样的字样,豁然开朗,是页面包车型大巴追踪日志,这几个私下认可是会输出来的。最终在艺术后边加了一个exit甘休就好了,下边是代码:

1.thinkphp导出csv文件

1.IndexController.class.php

导出csv文件可能就那几行代码,前天有个难点干扰本人好久,就是导出之后出现部分html代码,这么些不该,view里面是空的,controller中最终也不曾$this->display(),最终留神看到think_page_trace那样的字样,豁然开朗,是页面包车型客车追踪日志,那一个暗许是会输出来的。最终在形式前面加了贰个exit截至就好了,上面是代码:

<?php  namespace HomeController;  use ThinkController;  class IndexController extends Controller {        public function index(){          $hotel = M('keywords')->field('PageName,Page')->select();          $str = "关键字,名称n";          $str = iconv('utf-8','gb2312',$str);          $result = mysql_query("select PageName,Page from hotel_keywords");          while($row=mysql_fetch_array($result)){              $PageName = iconv('utf-8','gb2312',$row['PageName']);              $Page = iconv('utf-8','gb2312',$row['Page']);              $str .= $PageName.",".$Page."n";          }          $fileName = date('Ymd').'.csv';          $model = D('Keywords');          $model->export_csv($fileName,$str);          exit;      }  }

1.IndexController.class.php

2.KeywordsModel.class.php

<?php
namespace HomeController;
use ThinkController;
class IndexController extends Controller {
 public function index(){
  $hotel = M('keywords')->field('PageName,Page')->select();
  $str = "关键字,名称n";
  $str = iconv('utf-8','gb2312',$str);
  $result = mysql_query("select PageName,Page from hotel_keywords");
  while($row=mysql_fetch_array($result)){
   $PageName = iconv('utf-8','gb2312',$row['PageName']);
   $Page = iconv('utf-8','gb2312',$row['Page']);
   $str .= $PageName.",".$Page."n";
  }
  $fileName = date('Ymd').'.csv';
  $model = D('Keywords');
  $model->export_csv($fileName,$str);
  exit;
 }
}
<?php   namespace HomeModel;    use ThinkModel;    class KeywordsModel extends Model{            public function export_csv($filename, $data){          header("Content-type:text/csv");             header("Content-Disposition:attachment;filename=".$filename);             header('Cache-Control:must-revalidate,post-check=0,pre-check=0');             header('Expires:0');             header('Pragma:public');          echo $data;                }  }

2.KeywordsModel.class.php

玄奥就在IndexController.class.php代码最后一句的exit这里,假设不写那句,输出的excel里面有html源码,截图如下:

<?php 
namespace HomeModel;
use ThinkModel;
class KeywordsModel extends Model{
 public function export_csv($filename, $data){
  header("Content-type:text/csv"); 
  header("Content-Disposition:attachment;filename=".$filename); 
  header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
  header('Expires:0'); 
  header('Pragma:public');
  echo $data;

 }
}

 图片 1

神秘就在IndexController.class.php代码最终一句的exit这里,假若不写那句,输出的excel里面有html源码,截图如下:

2.用表格输出excel

图片 2

本文由澳门威利斯人发布于威利斯人娱乐,转载请注明出处:用表格输出excel,thinkPHP导出csv文件及用表格输出

关键词: 澳门威利斯人