欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

thinkPHP导出csv文件及用表格输出excel的方法

程序员文章站 2022-07-03 08:35:15
本文实例讲述了thinkphp导出csv文件及用表格输出excel的方法。分享给大家供大家参考,具体如下: 1.thinkphp导出csv文件 导出csv文件可能就那几...

本文实例讲述了thinkphp导出csv文件及用表格输出excel的方法。分享给大家供大家参考,具体如下:

1.thinkphp导出csv文件

导出csv文件可能就那几行代码,今天有个问题困扰我好久,就是导出之后出现一些html代码,这个不应该,view里面是空的,controller中最后也没有$this->display(),最后细心看到think_page_trace这样的字样,恍然大悟,是页面的跟踪日志,这个默认是会输出来的。最后在方法后面加了一个exit结束就好了,下面是代码:

1.indexcontroller.class.php

<?php
namespace home\controller;
use think\controller;
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;
 }
}

2.keywordsmodel.class.php

<?php 
namespace home\model;
use think\model;
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;
  
 }
}

奥妙就在indexcontroller.class.php代码最后一句的exit这里,如果不写这句,输出的excel里面有html源码,截图如下:

thinkPHP导出csv文件及用表格输出excel的方法

2.用表格输出excel

如下代码purchase_prospects.php

<?php
require('page_header.php');
$site_id = getifset($_get, 'site_id', 0);
$customer_type = getifset($_get, 'customer_type',0);
$db = database::connect($site_id);
if($site_id>0 && $customer_type>0){
 $sql = '';
 $out = ''; 
 $short_name_array = sitesettings::$site_short_name;
 $short_name = $short_name_array[$site_id]; 
 switch ($customer_type) {
  case '1':{
   $sql = "select 
      email, concat(ucase(left(firstname, 1)),substring(firstname, 2)) as firstname, concat(ucase(left(lastname, 1)),substring(lastname, 2)) as lastname 
     from customers 
     where site_id =$site_id 
      and email not regexp '.+(avanquest)|(planetart)|(novadevelop)|(qatest).+' 
      and email in(select a.email from customers a inner join orders b on a.id=b.`customer_id` and b.is_test=0 and a.site_id =$site_id) 
      and email not in (select email_address from `newsletter_unsubscribes` where site_id =$site_id);";
   $res = $db->query($sql);
   $out = '<table class="data_table"><tr><th>email</th><th>firstname</th><th>lastname</th></tr>';
   while($row = mysql_fetch_array($res)){
    $out .= '<tr><td>'.$row['email'].'</td><td>'.$row['firstname'].'</td><td>'.$row['lastname'].'</td></tr>';
   }
   $short_name .= '_purchased';
   break;
  }
  case '2':{
   $db->query("drop temporary table if exists tmp_purchase;");
   $db->query("create temporary table tmp_purchase select a.email from customers a inner join orders b on a.id=b.`customer_id` and b.is_test=0 and a.site_id =$site_id; ");
   $db->query("drop temporary table if exists tmp_nopurchase;");
   $db->query("create temporary table tmp_nopurchase select email from customers where site_id =$site_id and email not in(select email from tmp_purchase);");
   if(9 != $site_id){
    $datatype = sitesettings::getpurchasedatatype($site_id);
    $db->query("insert tmp_nopurchase select distinct email from triggered_email_data where datatype='$datatype' and email not in(select email from tmp_purchase);");
   }
   $sql = "select distinct email from tmp_nopurchase where email not regexp '.+(avanquest)|(planetart)|(novadevelop)|(qatest).+' and email regexp '[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$' and email not in (select email_address from `newsletter_unsubscribes` where site_id =$site_id);";
   $res = $db->query($sql);
   $out = '<table class="data_table"><tr><th>email</th></tr>';
   while($row = mysql_fetch_array($res)){
    $out .= '<tr><td>'.$row['email'].'</td></tr>';
   }
   $short_name .= '_non-purchased and signup';
   break;
  }
  default:
   break;
 }
 $out .= '</table>';
 header("content-type:application/vnd.ms-excel");
 header("content-disposition:filename=$short_name.xls");
 echo $out;
 exit;
}
?>
<h1>purchase prospects report</h1>
<form name="frm" method="get" action="purchase_prospects.php">
<strong>select site:</strong> 
<select name="site_id" id="site_id">
 <option value="0">== select site ==</option>
 <option value="1">sti</option>
 <option value="2">pa</option>
 <option value="3">cw</option>
 <option value="6">mcc</option>
 <option value="9">cb</option>
 <option value="4">stiuk</option>
 <option value="8">mccuk</option>
</select>   
   
<strong>select type:</strong>
<select id="customer_type" name="customer_type">
 <option value="0">== select type ==</option>
 <option value="1">purchased</option>
 <option value="2">non-purchased and signup</option>
</select>
  
   
<input id="submit" type="submit" value="run »">
</form>
<script type="text/javascript">
 $(function(){
  $("#submit").click(function(){
   if('0' == $("#site_id").val() || '0' == $("#customer_type").val()){
    alert('please select site and site');
    return false;
   }
  })
 });
</script>

这样也可以导出ecxcel文件,截图如下

thinkPHP导出csv文件及用表格输出excel的方法

依然注意最后一句exit;如果没有这一句,excel里面会有一些页面元素。

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。