PHP导出EXCEL

class toExcel{
    public $link = null;
    function __construct(){
    }
    /***************************************************************************
    * $mapping:数组格式头信息$map=array('No','Name','Email','Age');
    * $datalist:数据库查出来的结果集
    * $fileName:Excel文件名称
    * return:Excel格式文件
    **************************************************************************/
    public function toExcel($mapping,$datalist,$fileName) {
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=".iconv('utf-8', 'gb2312', $fileName).".xls");
        echo'<html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
        <head>
        <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
        <meta http-equiv=Content-Type content="text/html; charset=UTF-8">
        <!--[if gte mso 9]><xml>
        <x:ExcelWorkbook>
        <x:ExcelWorksheets>
        <x:ExcelWorksheet>
        <x:Name></x:Name>
        <x:WorksheetOptions>
        <x:DisplayGridlines/>
        </x:WorksheetOptions>
        </x:ExcelWorksheet>
        </x:ExcelWorksheets>
        </x:ExcelWorkbook>
        </xml><![endif]-->
        </head>
        <body link=blue vlink=purple leftmargin=0 topmargin=0>';
        echo'<table border="0" cellspacing="0" cellpadding="0">';
        echo'<tr>';
        if(is_array($mapping)) {
            foreach($mapping as $key=>$val)
            echo"<td style='font-weight:bold;'>".$val."</td>";
        }
        echo'</tr>';
        foreach($datalist as $k=>$v){
            echo'<tr>';
            foreach($v as $key=>$val){
                if(is_numeric($val) && strlen($val)>=14){
                    echo"<td style='vnd.ms-excel.numberformat:@'>".$val."</td>"; //大于14位的数字转换成字符串输出(如身份证)
                }else{
                    echo"<td>".$val."</td>";
                }
            }
            echo'</tr>';
        }
        echo'</table>';
        echo'</body>';
        echo'</html>';
    }
}
 
 
$map=array('No','Name','Email');
$datal=array(array('120760130129512044', '管理员', 'admin@163.com'), array('120760130129512055', 'member', 'member@163.com'));;
$csv=new toExcel;
$csv->toExcel($map,$datal,"dataexport"); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写代码的马良

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值