thinkphp Implementing Export

Source: Internet
Author: User

When I first started to do the project, I met this demand. To tell the truth, for a novice to come out of the internship, but also to contact the novice PHP, is really a heart to kill, powerless.

The simplest way, is to find a plug-in online, and then a more than 10 trillion project, and added more than two trillion of useless things. The main thing, directly with the plug-in, but also learn not. Online there are a lot of phpexcel plug-ins, and plug-ins related articles, but is not such a mistake is such a mistake, not the most important code is not added to, is that the description is not clear, in short, is a lot of problems.

Then, the dog blood came out, the great God help me to solve a lot of the problems I said above, and told me that this does not need to use plug-ins, as long as a few simple sections of the function can be. Tears, it took me long time time, is so simple a few sentences function. However, the function is simple, not necessarily the function is so simple.

Don't say much nonsense, start stripping code.

The first is the project folder (for example WAP), the path: wap/common/common/function.php (the Custom function is placed here and can be directly referenced).

  

/** * Export data for Excel table * @param $data a two-dimensional array, structured like an array from a database * @param $title the first row of Excel header, an array, if empty, there is no title * @param $filename Download filename * @examlpe * $stu = M (' User '); * $arr = $stu, select (); *exportexcel ($arr, array (' ID ', ' account ', ' Password ', ' nickname '), ' filename! '); */functionExportexcel ($data=Array(),$title=Array(),$filename= ' Themed Events '){    //var_dump ($data);d ie ();    Header("Content-type:application/octet-stream"); Header("Accept-ranges:bytes"); Header("Content-type:applicationnd.ms-excel"); Header("Content-disposition:attachment;filename=".$filename.". xls); Header("Pragma:no-cache"); Header("expires:0"); //Export xls start    if(!Empty($title)){        foreach($title  as $k=$v) {            $title[$k]=Iconv("UTF-8", "GB2312",$v); }        $title=implode("\ T",$title); Echo"$title\ n "; }        if(!Empty($data)){        foreach($data  as $key=$val){        foreach($val  as $ck=$CV) {        $data[$key][$ck]=Iconv("UTF-8", "GB2312",$CV); }        $data[$key]=implode("\ T",$data[$key]); }        Echo implode("\ n",$data); }}

  

Then there is the code for the control layer (activityuserController.class.php):

 /*  * * Export data to Excel table *@ $data to data * @ $TITL E_arr for file header * @file_name name  */ public
     function   Excel () {  $xlsModel  = D (' activityuser '   $data  =  $xlsModel ->getalluser (); Exportexcel (  $data , array  (' Player ID    ', ' team members ', ' names of players ', ' contact information ', ' identity '), ' theme activities '); //  method name () direct reference to dunction.php method } 

Then the Model layer (reading the corresponding data) (activityuserModel.class.php):

     Public functionGetalluser () {$UserInfo=m (' Activity_team_member '); $where[' Activity_delete ']=1;//Filter Conditions        $list=$UserInfo->query ('Select Ea_activity_team_member.id,ea_activity_team.team_name,user_name,user_tel,case user_identity when 0 then \ ' Captain ' when 1 then \ ' member \ ' end as user_identity from Ea_activity_activity,ea_activity_team,ea_activity_team_me Mber WHERE ea_activity_team.id=ea_activity_team_member.team_id and Ea_activity_activity.id=ea_activity_ team.activity_id‘); return $list; }

Next is the implementation of the HTML page:

<span class= "FR" >[<a href= "{: U (' Activityuser/excel ')}" > Export </a>]</span>

A simple comb, the first is to add a function.php file in the common folder, which is where the custom function exists. Put Excel's export function inside (directly to the code above).

At the controller level, define a method for exporting Excel, and define a method to read the model layer of the database. One thing to mention here is that I display different data (0 and 1 of the data) (to Baidu case), get the data from the database, and then use the Custom function (using: Custom Method ()).

The next thing to do is in the HTML page, do a <a></a> tag jump, jump to the control layer of their own method of definition.

thinkphp export is so simple, the code is not much, even less pathetic. However, the sharing of experience is more important.



thinkphp Implementing Export

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.