//前台 <button onclick="export_word()" >导出word</button> <script> function export_word(){ location.href = "{:url('Home/index/down_word')}" } </script> //后台 public function down_word(){ $row = Db::name('article')->find(1); $this->export_word($row['title'], $row['content']); } private function export_word($title,$content){ $content = str_replace("src=\"/", "src=\"http://www.XXX.com/", $content); //给是相对路径的图片加上域名变成绝对路径,导出来的word就会显示图片了 $filename = iconv('utf-8', 'gb2312', $title); header('pragma:public'); header('Content-type:application/vnd.ms-word;charset=utf-8;name="' . $filename . '".doc'); header("Content-Disposition:attachment;filename=$filename.doc"); //attachment新窗口打印inline本窗口打印 $html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'; //这句不能少,否则不能识别图片 echo $html . $content . '</html>'; }