這段是百度編輯器輸入的html代碼,自己保存下來

image.png
正文

image.png
然后導(dǎo)出word文檔
public function export($html )
{
//傳入html代碼
$file = time().rand(10000,99999);
$path = './Public/tmp/'.$file.'.doc'; //自定義路徑
//寫入內(nèi)容
file_put_contents($path,$html);
//下載文件
dowmload('/Public/tmp/'.$file.'.doc','管理評審會議紀(jì)要_' . changeDateTime(time()));
//刪除文件
@unlink($path);
//return C('dir_url').'/Public/tmp/'.$file.'.doc';
}
下載文件的方法
function dowmload($file, $newname = '')
{
// $filename = getFileUrl($file);
//C('dir_url') 根目錄
$filename = C('dir_url') . $file;
if (!file_exists($filename)) {
echo '<script>alert("文件不存在");history.go(-1);</script>';
exit();
}
$ua = $_SERVER["HTTP_USER_AGENT"];
$file_disk = pathinfo($filename, PATHINFO_EXTENSION);
ob_start();
if ($newname == '') {
$newname = date('Ymd-H:i:s');
}
$file_name = $newname . '.' . $file_disk;
/**解決中文亂碼**/
$encoded_filename = urlencode($file_name);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
//兼容IE11
if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $encoded_filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
}
/**解決中文亂碼** end**/
header('Content-Type: application/octet-stream');
header('Accept-Ranges : bytes');
// header("Content-Disposition:attachment;filename={$newname}.{$file_disk}");//聲明作為附件處理和下載后文件的名稱
$size = readfile($filename);
header("Accept-Length :" . $size);
}
然后發(fā)現(xiàn)下載的word文檔打開會默認(rèn)web板式瀏覽,改回頁面視圖只要把你富文本的html放在下面html內(nèi)就OK啦!就這么簡單!
<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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<xml><w:WordDocument><w:View>Print</w:View></xml>
<script src="includes/js/ztree/js/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<html>

image.png
如果想要在特定的模板內(nèi)插入富文本內(nèi)容可以先預(yù)設(shè)好模板的HTML在把富文本html插入預(yù)設(shè)好的html內(nèi)就行了!