php生成word文档在学习中可以经常见到,本篇将介绍一种方法。
PHP生成word文档,网上有很多方法,有调用COM组件生成的,有安装PHP扩展生成的,也有引用第三方类库,如phpword生成的。以下为最简洁的两种方法,无须安装其他,只要你安装了php环境便可以直接生成。
* @desc 方法一、生成word文档
* @param $content
* @param string $fileName
*/function createWord($content='',$fileName='new_file.doc'){ if(empty($content)){ return;
}
$content='
'.$content.''; if(empty($fileName)){
$fileName=date('YmdHis').'.doc';
}
$fp=fopen($fileName,'wb');
fwrite($fp,$content);
fclose($fp);
}
$str = '
我是h1
我是h2';
createWord($str);/**
* @desc 方法二、生成word文档并下载
* @param $content
* @param string $fileName
*/function downloadWord($content, $fileName='new_file.doc'){ if(empty($content)){ return;
}
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$fileName");
$html = '';
$html .= ''; echo $html . ''.$content .'';
}
$str = '
表头:姓名
电话
电话
Bill Gates
555 77 854
555 77 855';
downloadWord($str, 'abc.doc');
运行后的效果图
PHP生成word文档
PHP生成word文档
本篇介绍了php生成word文档的方法,更多相关内容请关注php中文网。
相关推荐: