PHP匯出word檔案,簡單拓展可匯出其他文字類檔案
阿新 • • 發佈:2018-11-05
PHP匯出word檔案,簡單拓展可匯出其他文字類檔案
/** * PHP 匯出簡單文字內容(word txt等) * @param $content mixed 匯出內容 (文字string / html程式碼) * @param $filename string 需儲存檔名 * @param string $extension 檔案型別 (doc docx txt xml) */ function export_html_to_word($content, $filename, $extension = 'doc') { ob_start(); $_export_content = ''; if ($extension == 'doc' || $extension == 'docx') { $_export_content .= '<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">'; } $_export_content .= $content; if ($extension == 'doc' || $extension == 'docx') $_export_content .= '</html>'; echo $_export_content; ob_get_contents(); if ($extension == 'doc' || $extension == 'docx') header('Content-type:application/word'); header('Content-Disposition: attachment; filename=' . $filename . '.' . $extension); ob_flush(); flush(); } $html = '<b style="color: red">你看我哪裡像好人</b>'; $wordname = 'test-file'; export_html_to_word($html, $wordname);