现在贴出部分代码,如需完整使用代码,请参照如下博客: https://www.waytomilky.com/archives/210.html
//XLS导出
public function ExcelPull($name, $header, $dataResult)
{
$headTitle = "xxxx";
$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);
} 