第一种
$content = //html代码;
$data = '<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">'.$content.'</html>';
file_put_contents($filename.$tem_title . ".doc", $data);
第二种 自动判断转excel 和word 原文http://www.thinkphp.cn/topic/26250.html
- function downloadWord($content, $file='newfile.xls'){
- header("Cache-Control: no-cache, must-revalidate");
- header("Pragma: no-cache");
- header("Content-Type: application/octet-stream");
- header("Content-Disposition: attachment; filename=$file");
- $ext = substr(end(explode('.', $file)), 0, 3);
- switch($ext){
- case 'doc' :
- $html = '<html xmlns:v="urn:schemas-microsoft-com:vml"xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"xmlns="http://www.w3.org/TR/REC-html40">';
- $html .= '<head></head>';
- break;
- case 'xls':
- $html = '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">';
- $html .= '<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>';
- }
- echo $html . '<body>'.$content .'</body></html>';
-
- }
- function index(){
- downloadWord($this->fetch('excel'), 'abc.xls');
- }