wenhainan 2018-10-20 08:01:19 7097次浏览 3条评论 6 1 0

现在贴出部分代码,完整使用代码请参考: https://www.waytomilky.com/index.php/archives/210/

//XLS导出
/**
$name  string 文件名称
$header array 列标题
$dataResult  数组
**/
public  function ExcelPull($name, $header, $dataResult)
{
    //这一行没啥用,根据具体情况优化下
    $headTitle = "xx详情";
    $headtitle= "<tr style='height:50px;border-style:none;><td border=\"0\" style='height:90px;width:470px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";
    $titlename = "<tr>";
    foreach ($header as $v) {
        $titlename .= "<td>$v</td>";
    }
    $titlename .= "</tr>";
    $fileName = date("Y-m-d") . "-" . $name . ".xls";
    $this->excelData($dataResult, $titlename, $headtitle, $fileName);
}


public  function excelData($data, $titlename, $title, $filename)
{
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
    $str .="<table border=1>" . $titlename;
    $str .= '';
    foreach ($data as $key => $rt) {
        $str .= "<tr>";
        foreach ($rt as $v) {
            $str .= "<td >{$v}</td>";
        }
        $str .= "</tr>\n";
    }
    $str .= "</table></body></html>";
    $str .= "<span>creator:".yii::$app->user->identity->user_loginname."</span>";
    header("Content-Type: application/vnd.ms-excel; name='excel'");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    header("Expires: 0");
    exit($str);
}
觉得很赞
您需要登录后才可以评论。登录 | 立即注册