Home » 经验&资源分享 » ThinkPHP导出Excel表格

ThinkPHP导出Excel表格

今天我分享下关于ThinkPHP中导出Excel表格的方法。最近的几篇ThinkPHP的教程我都是直接贴代码,我觉得还是比较好理解的,不懂的朋友可以去查一下ThinkPHP的相关教程。

首先在项目的Action目录中新建一个pageAction.class.php文件,输入如下代码

<span id="lnum1" style="color: #606060;"> 1:</span> &lt;?php

<span id="lnum2" style="color: #606060;"> 2:</span>     <span style="color: #0000ff;">class</span> pageAction <span style="color: #0000ff;">extends</span> Action{

<span id="lnum3" style="color: #606060;"> 3:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> excel() {

<span id="lnum4" style="color: #606060;"> 4:</span>                $date1=<span style="color: #006080;">"2013-08-06"</span>;

<span id="lnum5" style="color: #606060;"> 5:</span>                $in1=<span style="color: #0000ff;">array</span>(1,0);

<span id="lnum6" style="color: #606060;"> 6:</span>                $out1=<span style="color: #0000ff;">array</span>(2,0);

<span id="lnum7" style="color: #606060;"> 7:</span>                $gates_name=<span style="color: #0000ff;">array</span>(<span style="color: #006080;">'支点咖啡一号门'</span>, <span style="color: #006080;">'支点咖啡二号门'</span>);

<span id="lnum8" style="color: #606060;"> 8:</span>                $sum_in=1;

<span id="lnum9" style="color: #606060;"> 9:</span>                $sum_out=2;

<span id="lnum10" style="color: #606060;"> 10:</span>                $sum_inout=<span style="color: #0000ff;">array</span>(3,0);

<span id="lnum11" style="color: #606060;"> 11:</span>                <span style="color: #0000ff;">if</span>(session(<span style="color: #006080;">'?date1'</span>)){

<span id="lnum12" style="color: #606060;"> 12:</span>                    session(<span style="color: #0000ff;">null</span>);

<span id="lnum13" style="color: #606060;"> 13:</span>                }

<span id="lnum14" style="color: #606060;"> 14:</span>                 session(<span style="color: #006080;">'date1'</span>,$date1);

<span id="lnum15" style="color: #606060;"> 15:</span>                 session(<span style="color: #006080;">'in1'</span>,$in1);

<span id="lnum16" style="color: #606060;"> 16:</span>                 session(<span style="color: #006080;">'out1'</span>,$out1);

<span id="lnum17" style="color: #606060;"> 17:</span>                 session(<span style="color: #006080;">'gates_name'</span>,$gates_name);

<span id="lnum18" style="color: #606060;"> 18:</span>                 session(<span style="color: #006080;">'sum_in'</span>,$sum_in);

<span id="lnum19" style="color: #606060;"> 19:</span>                 session(<span style="color: #006080;">'sum_out'</span>,$sum_out);

<span id="lnum20" style="color: #606060;"> 20:</span>                 session(<span style="color: #006080;">'sum_inout'</span>,$sum_inout);

<span id="lnum21" style="color: #606060;"> 21:</span>                 $this-&gt;display();

<span id="lnum22" style="color: #606060;"> 22:</span>         }

<span id="lnum23" style="color: #606060;"> 23:</span>     }<span style="color: #008000;">//类定义 end</span>

<span id="lnum24" style="color: #606060;"> 24:</span> ?&gt;

然后新建一个ExcelAction.class.php文件,输入如下代码

<span id="lnum1" style="color: #606060;"> 1:</span> &lt;?php

<span id="lnum2" style="color: #606060;"> 2:</span>     <span style="color: #0000ff;">class</span> ExcelAction <span style="color: #0000ff;">extends</span> Action{

<span id="lnum3" style="color: #606060;"> 3:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> index () {

<span id="lnum4" style="color: #606060;"> 4:</span>             $date1 = session(<span style="color: #006080;">'date1'</span>);

<span id="lnum5" style="color: #606060;"> 5:</span>             $in1=session(<span style="color: #006080;">'in1'</span>);

<span id="lnum6" style="color: #606060;"> 6:</span>             $out1=session(<span style="color: #006080;">'out1'</span>);

<span id="lnum7" style="color: #606060;"> 7:</span>             $gates_name=session(<span style="color: #006080;">'gates_name'</span>);

<span id="lnum8" style="color: #606060;"> 8:</span>             $sum_in=session(<span style="color: #006080;">'sum_in'</span>);

<span id="lnum9" style="color: #606060;"> 9:</span>             $sum_out=session(<span style="color: #006080;">'sum_out'</span>);

<span id="lnum10" style="color: #606060;"> 10:</span>             $sum_inout=session(<span style="color: #006080;">'sum_inout'</span>);

<span id="lnum11" style="color: #606060;"> 11:</span>             <span style="color: #0000ff;">if</span>(!$sum_inout) halt(<span style="color: #006080;">'页面不存在'</span>);

<span id="lnum12" style="color: #606060;"> 12:</span>             header(<span style="color: #006080;">"Content-type:application/vnd.ms-excel;"</span>);

<span id="lnum13" style="color: #606060;"> 13:</span>             header(<span style="color: #006080;">"Content-Disposition:filename=日门客流分时情况.xls"</span>);

<span id="lnum14" style="color: #606060;"> 14:</span>

<span id="lnum15" style="color: #606060;"> 15:</span>             <span style="color: #0000ff;">echo</span> <span style="color: #006080;">"\t"</span>;

<span id="lnum16" style="color: #606060;"> 16:</span>             <span style="color: #0000ff;">echo</span> $date1.iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,<span style="color: #006080;">'日门客流分时情况'</span>).<span style="color: #006080;">"\n"</span>;

<span id="lnum17" style="color: #606060;"> 17:</span>             <span style="color: #0000ff;">echo</span> <span style="color: #006080;">"\t"</span>;

<span id="lnum18" style="color: #606060;"> 18:</span>             $i=0;

<span id="lnum19" style="color: #606060;"> 19:</span>             <span style="color: #0000ff;">while</span>($gates_name[$i]){

<span id="lnum20" style="color: #606060;"> 20:</span>                 <span style="color: #0000ff;">echo</span> iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,$gates_name[$i]).<span style="color: #006080;">"\t"</span>;

<span id="lnum21" style="color: #606060;"> 21:</span>                 $i++;

<span id="lnum22" style="color: #606060;"> 22:</span>             }

<span id="lnum23" style="color: #606060;"> 23:</span>             <span style="color: #0000ff;">echo</span> iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,<span style="color: #006080;">'累计'</span>).<span style="color: #006080;">"\t\n"</span>;

<span id="lnum24" style="color: #606060;"> 24:</span>             <span style="color: #0000ff;">echo</span> iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,<span style="color: #006080;">'进门人数'</span>).<span style="color: #006080;">"\t"</span>;

<span id="lnum25" style="color: #606060;"> 25:</span>             $i=0;

<span id="lnum26" style="color: #606060;"> 26:</span>             <span style="color: #0000ff;">while</span>($gates_name[$i]){

<span id="lnum27" style="color: #606060;"> 27:</span>                 <span style="color: #0000ff;">echo</span> $in1[$i].<span style="color: #006080;">"\t"</span>;

<span id="lnum28" style="color: #606060;"> 28:</span>                 $i++;

<span id="lnum29" style="color: #606060;"> 29:</span>             }

<span id="lnum30" style="color: #606060;"> 30:</span>             <span style="color: #0000ff;">echo</span> $sum_in.<span style="color: #006080;">"\t\n"</span>;

<span id="lnum31" style="color: #606060;"> 31:</span>             <span style="color: #0000ff;">echo</span> iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,<span style="color: #006080;">'出门人数'</span>).<span style="color: #006080;">"\t"</span>;

<span id="lnum32" style="color: #606060;"> 32:</span>             $i=0;

<span id="lnum33" style="color: #606060;"> 33:</span>             <span style="color: #0000ff;">while</span>($gates_name[$i]){

<span id="lnum34" style="color: #606060;"> 34:</span>                 <span style="color: #0000ff;">echo</span> $out1[$i].<span style="color: #006080;">"\t"</span>;

<span id="lnum35" style="color: #606060;"> 35:</span>                 $i++;

<span id="lnum36" style="color: #606060;"> 36:</span>             }

<span id="lnum37" style="color: #606060;"> 37:</span>             <span style="color: #0000ff;">echo</span> $sum_out.<span style="color: #006080;">"\t\n"</span>;

<span id="lnum38" style="color: #606060;"> 38:</span>             <span style="color: #0000ff;">echo</span> iconv (<span style="color: #006080;">'utf-8'</span>,<span style="color: #006080;">'gb2312'</span>,<span style="color: #006080;">'累计'</span>).<span style="color: #006080;">"\t"</span>;

<span id="lnum39" style="color: #606060;"> 39:</span>             $i=0;

<span id="lnum40" style="color: #606060;"> 40:</span>             <span style="color: #0000ff;">while</span>($gates_name[$i]){

<span id="lnum41" style="color: #606060;"> 41:</span>                 <span style="color: #0000ff;">echo</span> $sum_inout[$i].<span style="color: #006080;">"\t"</span>;

<span id="lnum42" style="color: #606060;"> 42:</span>                 $i++;

<span id="lnum43" style="color: #606060;"> 43:</span>             }

<span id="lnum44" style="color: #606060;"> 44:</span>             <span style="color: #0000ff;">echo</span> <span style="color: #006080;">"\n"</span>;

<span id="lnum45" style="color: #606060;"> 45:</span>         }

<span id="lnum46" style="color: #606060;"> 46:</span>     }<span style="color: #008000;">//类定义 end</span>

<span id="lnum47" style="color: #606060;"> 47:</span> ?&gt;

需要注意的是所有的汉字都需要转换成gb2312的编码,这样导出的excel文件才不会出现中文乱码的情况。

Continue reading...
分享