thinkphp Exporting a CSV file and using a tabular method to output Excel _php instance

Source: Internet
Author: User
This article describes the thinkphp export CSV file and the way to output Excel with a table. Share to everyone for your reference, as follows:

1.thinkphp Export CSV file

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

1.indexcontroller.class.php

<?phpnamespace 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 the last sentence of the IndexController.class.php code here, if you do not write this sentence, the output of Excel has HTML source code, as follows:

2. Output Excel with a table

The following code purchase_prospects.php

<?phprequire (' 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 (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 = '
 
 '; while ($row = Mysql_fetch_array ($res)) {$out. = ' 
   '; } $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 joins 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 e-mail from tmp_purchase); "); $sql = "Select DISTINCT email from tmp_nopurchase WHERE email is 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 = ' 
  
Email FirstName LastName
'. $row [' email ']. ' '. $row [' FirstName ']. ' '. $row [' LastName ']. '
'; while ($row = Mysql_fetch_array ($res)) {$out. = ' '; } $short _name. = ' _non-purchased and signup '; Break } Default:break; } $out. = '
Email
'. $row [' email ']. '
'; Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:filename= $short _name.xls"); Echo $out; Exit;}? >

Purchase Prospects Report

This also allows you to export the Ecxcel file as follows

Still pay attention to the last sentence exit; If you do not have this sentence, Excel will have some page elements.

It is hoped that this article is helpful to 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.