wenhainan 2018-10-20 08:01:19 393次浏览 1条评论 0 0 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);
}
亿速云
  • 评论于 2018-12-11 09:56

    这个主要适合生成单文件单sheet的场景下,不过这种场景有更多的生成方式,比如table直接通过js导出excel,连后端请求都没有了。如果单文件多sheet的情况,目前来看还是得用效率确实差到极点的phpexcel

    1 条回复
    评论于 2018-12-12 21:36

    是的,看场景

您需要登录后才可以评论。登录 | 立即注册