thinkphp export CSV file and table output Excel method _php Instance

Source: Internet
Author: User

The example in this article describes the thinkphp export CSV file and the way to output excel in a table. Share to everyone for your reference, specific as follows:

1.thinkphp Export CSV file

Export CSV file may be a few lines of code, today has a problem for me for a long time, that is, after the export of some HTML code, this should not, view inside is empty, controller in the end there is no $this->display (), and finally careful to see THINK_ Page_trace such words, suddenly, is the page tracking log, this default will be output. Finally, add an exit end to the method, and here's the code:

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 = "keyword, name \ 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;
  
 }
}

The secret is in IndexController.class.php code the last sentence of exit here, if not write this sentence, the output of Excel has HTML source code, screenshot as follows:

2. Output Excel with Table

The following code 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 (Firstnam E, 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 to 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 = $si
   TE_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 isn't 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 }?>  

This can also be exported ecxcel file, screenshot as follows

Still pay attention to the last sentence exit; if not, there will be some page elements in Excel.

I hope this article will help you with the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.