• php 根据html table生成excel文件


     1 <?php
     2 
     3 /*
     4 *处理Excel导出
     5 *@param $datas array 设置表格数据
     6 *@param $titlename string 设置head
     7 *@param $title string 设置表头
     8 */
     9 function excelData($datas, $titlename, $title, $filename) {
    10     $str = "<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    </head>
    <body>";
    11     $str .= "<table border=1><head>" . $titlename . "</head>";
    12     $str .= $title;
    13     foreach ($datas as $key => $rt) {
    14         $str .= "<tr>";
    15         foreach ($rt as $k => $v) {
    16             $str .= "<td>{$v}</td>";
    17         }
    18         $str .= "</tr>
    ";
    19     }
    20     $str .= "</table></body></html>";
    21     echo $str;
    22     header("Content-Type: application/vnd.ms-excel; name='excel'");
    23     header("Content-type: application/octet-stream");
    24     header("Content-Disposition: attachment; filename=" . $filename);
    25     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    26     header("Pragma: no-cache");
    27     header("Expires: 0");
    28     exit($str);
    29 }
    30 
    31 
    32 $dataResult = array(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    33 $headTitle = "XX保险公司 优惠券赠送记录";
    34 $title = "优惠券记录";
    35 $headtitle = "<tr style='height:50px;border-style:none;><th border="0" style='height:60px;270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";
    36 $titlename = "<tr> 
    37                <th style='70px;' >合作商户</th> 
    38                <th style='70px;' >会员卡号</th> 
    39                <th style='70px;'>车主姓名</th> 
    40                <th style='150px;'>手机号</th> 
    41                <th style='70px;'>车牌号</th> 
    42                <th style='100px;'>优惠券类型</th> 
    43                <th style='70px;'>优惠券名称</th> 
    44                <th style='70px;'>优惠券面值</th> 
    45                <th style='70px;'>优惠券数量</th> 
    46                <th style='70px;'>赠送时间</th> 
    47                <th style='90px;'>截至有效期</th> 
    48            </tr>";
    49 $filename = $title . ".xls";
    50 excelData($dataResult, $titlename, $headtitle, $filename);
    51 echo 'success';
  • 相关阅读:
    mysql5.6 online ddl—索引
    lepus监控OS配置
    mysql字符集问题
    xtrabackup 2.0.8备份mysql5.1.65报错
    子网掩码值
    僵尸进程(待补充)
    libc.so.6重做链接,删除导致的缺失问题(后期需要深入研究),未能成功升级
    atlas
    ./encrypt: error while loading shared libraries: libcrypto.so.10:
    lepus bug
  • 原文地址:https://www.cnblogs.com/dannywang/p/7644729.html
Copyright © 2020-2023  润新知