美文网首页PHP实战PHP值圈PHP
PHP完美生成word文档,可加入html元素

PHP完美生成word文档,可加入html元素

作者: whmblog | 来源:发表于2018-01-24 00:16 被阅读160次

PHP生成word文档,网上有很多方法,有调用COM组件生成的,有安装PHP扩展生成的,也有引用第三方类库,如phpword生成的。以下为最简洁的两种方法,无须安装其他,只要你安装了php环境便可以直接生成。

<?php

header("Content-type:text/html;charset=utf-8");

/**
 * @desc 方法一、生成word文档
 * @param $content
 * @param string $fileName
 */
function createWord($content='',$fileName='new_file.doc'){
    if(empty($content)){
        return;
    }
    $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">
            <meta charset="UTF-8" />'.$content.'</html>';
    if(empty($fileName)){
        $fileName=date('YmdHis').'.doc';
    }
    $fp=fopen($fileName,'wb');
    fwrite($fp,$content);
    fclose($fp);
}

$str = '<h1 style="color:red;">我是h1</h1><h2>我是h2</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 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><meta charset="UTF-8" /></head>';

    echo $html . '<body>'.$content .'</body></html>';

}

$str = '<h4>表头:</h4>
<table border="1">
<tr>
  <th>姓名</th>
  <th>电话</th>
  <th>电话</th>
</tr>
<tr>
  <td>Bill Gates</td>
  <td>555 77 854</td>
  <td>555 77 855</td>
</tr>
</table>';

downloadWord($str, 'abc.doc');

运行后的效果图

PHP生成word文档
PHP生成word文档

转载请标明原文链接: https://www.jianshu.com/p/3a59904c6898
更多精彩请访问个人博客:https://www.whmblog.cn/

相关文章

  • PHP完美生成word文档,可加入html元素

    PHP生成word文档,网上有很多方法,有调用COM组件生成的,有安装PHP扩展生成的,也有引用第三方类库,如ph...

  • DOM简介

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 ** HTML DOM (文档对...

  • 2018-08-21

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型...

  • 四、JavaScript HTML DOM

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型...

  • HTML JS DOM

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型...

  • JavaScript 02 HTML DOM

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型...

  • JavaScript DOM编程(1)修改html css

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模...

  • JavaScript HTML DOM

    通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型...

  • DOM基础整理 - 1

    1 通过HTML DOM,可访问JavaScript HTML文档的所有元素。 2 HTML DOM(文档对象模型...

  • PHPWord使用简介

    PHP导出word文档,可实现自动分页,可插入图片,表格。 1、下载PHPWord(1)使用composer: (...

网友评论

    本文标题:PHP完美生成word文档,可加入html元素

    本文链接:https://www.haomeiwen.com/subject/ftfyaxtx.html