dd

实现导出表格为xls的简单方法

jerry thinkphp 2015年11月18日 收藏
很多时候,我们需要输出到网页上的表格内容提供导出为excel的功能。这是一个最省事,最简单的方法。重点只需两行代码!
function xls()
    {
        //$this->getpoview();//此处是数据获得方法
        $filename=$_GET['id'];
        header('application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
        $this->display();
    }
模板文件如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
table{border-collapse:collapse;width:1000px;}
.title th{border:1px solid #ccc;padding:5px;background:#eee;}
tbody td{border:1px solid #ccc;padding:5px;}
</style>
</head>
<body>
<TABLE cellpadding="2" cellspacing="0">
<thead>
<TR>
    <th width="200px"></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
</TR>
<TR>
    <th colspan="6"><h1 class="text-center"><eq name="list.style" value="0">{$Think.lang.buypo}</eq>
    <eq name="list.style" value="1">{$Think.lang.sellpo}</eq></h1></th>
</TR>
<TR>
    <th align="right" width="120px">{$Think.lang.no}</th>
    <td colspan="2">{$list.guid}</td>
    <th align="right">{$Think.lang.date}</th>
    <td colspan="2">{$list.tdate}</td>
</TR>
<TR>
    <th align="right">{$Think.lang.company}</th>
    <td colspan="2">{$list.company}</td>
    <th align="right">{$Think.lang.telphone}</th>
    <td colspan="2">{$list.phone}</td>
</TR>
<TR>
    <th align="right">{$Think.lang.mobi}</th>
    <td colspan="2">{$list.mb}</td>
    <th align="right">{$Think.lang.email}</th>
    <td colspan="2">{$list.email}</td>
</TR>
<TR>
    <th align="right">{$Think.lang.qq}</th>
    <td colspan="2">{$list.qq}</td>
    <th align="right">{$Think.lang.address}</th>
    <td colspan="2">{$list.address}</td>
</TR>
<TR class="title">
    <th width="120px" bgcolor="#CCCCCC">{$Think.lang.pic}</th>
    <th bgcolor="#CCCCCC">{$Think.lang.info}</th>
    <th bgcolor="#CCCCCC">{$Think.lang.desc}</th>
    <th bgcolor="#CCCCCC">{$Think.lang.usefor}</th>
    <th bgcolor="#CCCCCC">{$Think.lang.price}</th>
    <th bgcolor="#CCCCCC">{$Think.lang.num}</th>
</TR></thead><tbody>
<volist name="row" id="v">
    <TR val="{$v.id}" <eq name='v["status"]' value='0'>class="ajax tr" url="__APP__/Ajax/getpd" func="showtd"</eq>>
        <TD class="pic"><a href="{$v.href}" target="_blank"><img src="{$v.pic|picpath}"></a></TD>
        <TD class="info">
            <b>{$v.name}</b><br style="mso-data-placement:same-cell;"/>
            {$v.search_fac}:{$v.search_num}<br style="mso-data-placement:same-cell;"/>
            {$Think.lang.numoth}<br style="mso-data-placement:same-cell;"/>
            {$v.othernum|excelbr}
        </TD>
        <TD class="desc pre">{$v.desc|excelbr}</TD>
        <TD class="usefor pre">{$v.userfor|excelbr}</TD>
        <TD class="price">{$v.price}</TD>
        <TD class="num">{$v.num}</TD>
    </TR>
</volist>
</tbody>
<tfoot>
<TR>
    
    <td colspan="4"></td>
    <td>{$list.totalprice}</td>
    <td>{$list.totalnum}</td>
</TR>
<TR>
    <td colspan="4"></td>
    <td colspan="2">{$Think.lang.total}{$list.total} </td>
</TR>
</tfoot>
</TABLE>
</body>
</html>
 
具体示例,请参看以下网址:http://tou.mobi/v/21cd67390173f6a5

dd