How to export a csv file and use a table to export an excel file using thinkPHP

Source: Internet
Author: User
Tags exit in
This article mainly introduces thinkPHP's method of exporting csv files and outputting excel files using tables. it involves thinkPHP's operations on tables and Excel files, for more information about how to export a csv file from thinkPHP and use a table to output an excel file, see the following example. We will share this with you for your reference. The details are as follows:

1. thinkphp exports csv files

The exported csv file may have just a few lines of code. today I have a problem for a long time, that is, some html code is generated after export. This should not be because the view is empty, the controller does not end with $ this-> display (). at last, we carefully read think_page_trace and suddenly realized that it is the page tracking log, which is output by default. Finally, add an exit after the method. the following code is used:

1. IndexController. class. php

<? Phpnamespace Home \ Controller; use Think \ Controller; class IndexController extends Controller {public function index () {$ hotel = M ('keyword')-> field ('pagename, page ') -> select (); $ str = "keyword, name \ n"; $ str = iconv ('utf-8', 'gb2312 ', $ str ); $ result = mysql_query ("select PageName, Page from cmd_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 ('keyword'); $ 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 lies in exit in the last sentence of IndexController. class. php code. if you do not write this sentence, the output excel file contains the html source code, as shown below:

2. output excel in a table

The following code is used: 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 (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. email FROM customers a inner join orders B ON. id = B. 'customer _ id' AND B. is_test = 0 AND. 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. email FROM customers a inner join orders B ON. id = B. 'customer _ id' AND B. is_test = 0 AND. site_id = $ site_id; "); $ DB-> query (" drop temporary table if exists tmp_nopurchase; "); $ DB-> query (" create temporary table tmp_nopurchase SELECT Email from mers where site_id = $ site_id AND email not in (SELECT email FROM tmp_purchase); "); if (9! = $ Site_id) {$ datatype = sitesetype: getPurchaseDataType ($ site_id ); $ DB-> query ("INSERT tmp_nopurchase select distinct email FROM triggered_email_data WHERE datatype = '$ Ype' 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 =' 
  
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

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.