{"id":240,"date":"2021-03-09T00:26:25","date_gmt":"2021-03-08T16:26:25","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=240"},"modified":"2021-03-09T00:26:25","modified_gmt":"2021-03-08T16:26:25","slug":"%e4%bd%bf%e7%94%a8phpword%e8%8e%b7%e5%8f%96doc%e4%b8%ad%e7%9a%84%e8%a1%a8%e6%a0%bc%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/blog.yuekegu.com\/?p=240","title":{"rendered":"\u4f7f\u7528phpword\u83b7\u53d6doc\u4e2d\u7684\u8868\u683c\u6570\u636e"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. \u9996\u5148\u786e\u5b9a\u4f7f\u7528phpword\u662f\u53ef\u4ee5\u8bfb\u53d6word\u6587\u6863\u4e2d\u8868\u683c\u91cc\u9762\u7684\u6570\u636e, \u4f7f\u7528\u7684phpword\u7248\u672c0.17.0<\/h3>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td><code>composer require <\/code><code>\"phpoffice\/phpword\"<\/code><\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u5173\u952e\u8bcd\u53ca\u5305\u542b\u539f\u7406 (\u8fd9\u91cc\u53ea\u505a\u7b80\u5355\u6982\u8ff0)<\/h3>\n\n\n\n<p>\u672c\u4eba\u505a\u535a\u6587\u559c\u6b22\u76f4\u63a5\u8d34\u4ee3\u7801\uff0c\u76f4\u63a5\u7528\u3002\u53ef\u8fd9\u4e2a\u4e0d\u592a\u884c\u554a\uff0c\u56e0\u4e3a\u8f93\u5165\u4e0d\u7edf\u4e00\uff0cword\u683c\u5f0f\u6837\u5f0f\u592a\u591a\uff0c\u8f93\u51fa\u4e5f\u4e0d\u7edf\u4e00\uff0c\u6709\u7684\u8981\u8f93\u51fa\u6570\u636e\uff0c\u6709\u7684\u8981\u8f93\u51faword\u6216\u8005html\uff0c\u6240\u4ee5\u8fd9\u91cc\u5c31\u8ffd\u4e00\u4e0b\u539f\u7406(\u53ef\u80fd\u4e0d\u4e25\u8c28\u4f46\u662f\u4fbf\u4e8e\u7406\u89e3)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u8bcd<\/h3>\n\n\n\n<p><strong>section(\u90e8\u5206)<\/strong> :&nbsp;phpword\u4e2d\u5c06word\u6587\u6863\u5206\u4e3a\u82e5\u5e72\u4e2asection(\u90e8\u5206)<\/p>\n\n\n\n<p><strong>element(\u5143\u7d20)<\/strong> :&nbsp;\u6bcf\u4e2asection\u5305\u542b\u82e5\u5e72\u4e2aelement(\u5143\u7d20)\u3001\u6587\u672c\u3001\u56fe\u7247\uff0c\u5143\u7d20\u5206\u4e3a\u6587\u672c\u5143\u7d20\u3001\u8868\u683c\u5143\u7d20\u3001\u5176\u4ed6\uff08\u672a\u6d89\u53ca\u4e0d\u505a\u8ba8\u8bba\uff09<\/p>\n\n\n\n<p><strong>textRun(\u6587\u672c\u5143\u7d20)<\/strong> :&nbsp;\u6bcf\u4e2a\u6587\u672c\u96c6\u5408\u5305\u542b\u591a\u4e2a\u6587\u672c<\/p>\n\n\n\n<p><strong>text(\u6587\u672c)<\/strong> : \u4e3a\u5b57\u7b26\u6216\u8005\u56fe\u7247<\/p>\n\n\n\n<p><strong>table(\u8868\u683c\u5143\u7d20)<\/strong> : \u6bcf\u4e2a\u8868\u683c\u5143\u7d20\u5305\u542b\u591a\u4e2a\u884c row<\/p>\n\n\n\n<p><strong>row(\u884c)<\/strong> : \u6bcf\u4e2a\u884c\u5305\u542b\u591a\u4e2a\u5217 cell<\/p>\n\n\n\n<p><strong>cell(\u5217)<\/strong> : \u6bcf\u4e2a\u5217\u5305\u542b\u591a\u4e2atextRun(\u6587\u672c\u5143\u7d20) \u8fd9\u91cc\u6ca1\u9519\uff0c\u5c31\u662f\u5305\u542b\u591a\u4e2a\u6587\u672c\u5143\u7d20\uff08\u8868\u683c\u5143\u7d20\u4e5f\u53ef\u4ee5\u4f46\u662f\u6ca1\u4eba\u5728word\u8868\u683c\u7684\u67d0\u4e00\u4e2a\u683c\u91cc\u518d\u6765\u4e00\u4e2a\u8868\u683c\u5427\uff09<\/p>\n\n\n\n<p>\u5404\u4e2a\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\u56fe<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4ee3\u7801\u5b9e\u73b0(\u672c\u5730\u6d4b\u8bd5\u5df2\u901a)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n * Created by PhpStorm.\n * User: parker\n * Date: 2020\/10\/18\n * Time: 16:09\n *\/\n\nnamespace common\\services;\n\n\nclass WordService extends BaseService\n{\n\n    public static function importWord($info)\n    {\n        $word  = self::getWord($info['path']);\n        dd($word);\n    }\n\n    \/**\n     * \u83b7\u53d6word\u6587\u6863\u5185\u5bb9\n     * @param string $path\n     * @return array\n     *\/\n    public static function getWord($path = '')\n    {\n        \/\/\u52a0\u8f7dword\u6587\u6863\uff0c\u4f7f\u7528phpword\u5904\u7406\n        $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($path);\n        return self::getNodeContent($phpWord);\n    }\n\n    \/**\n     * \u6839\u636eword\u4e3b\u8282\u70b9\u83b7\u53d6\u5206\u8282\u70b9\u5185\u5bb9\n     * @param $word\n     * @return array\n     *\/\n    public static function getNodeContent($word)\n    {\n        $return = [];\n        \/\/\u5206\u89e3\u90e8\u5206\n        foreach ($word->getSections() as $section)\n        {\n            if ($section instanceof \\PhpOffice\\PhpWord\\Element\\Section) {\n                \/\/\u5206\u89e3\u5143\u7d20\n                foreach ($section->getElements() as $element)\n                {\n                    \/\/\u6587\u672c\u5143\u7d20\n                    if ($element instanceof \\PhpOffice\\PhpWord\\Element\\TextRun) {\n                        $text = '';\n                        foreach ($element->getElements() as $ele) {\n                            $text .= self::getTextNode($ele);\n                        }\n                        $return[] = $text;\n                    }\n                    \/\/\u8868\u683c\u5143\u7d20\n                    else if ($element instanceof \\PhpOffice\\PhpWord\\Element\\Table) {\n                        foreach ($element->getRows() as $ele)\n                        {\n                            $return[] = self::getTableNode($ele);\n                        }\n                    }\n                }\n            }\n        }\n        return $return;\n    }\n\n    \/**\n     * \u83b7\u53d6\u6587\u6863\u8282\u70b9\u5185\u5bb9\n     * @param $node\n     * @return string\n     *\/\n    public static function getTextNode($node)\n    {\n        $return = '';\n        \/\/\u5904\u7406\u6587\u672c\n        if ($node instanceof \\PhpOffice\\PhpWord\\Element\\Text)\n        {\n            $return .= $node->getText();\n        }\n        \/\/\u5904\u7406\u56fe\u7247\n        else if ($node instanceof \\PhpOffice\\PhpWord\\Element\\Image)\n        {\n            $return .= self::pic2text($node);\n        }\n        \/\/\u5904\u7406\u6587\u672c\u5143\u7d20\n        else if ($node instanceof \\PhpOffice\\PhpWord\\Element\\TextRun) {\n            foreach ($node->getElements() as $ele) {\n                $return .= self::getTextNode($ele);\n            }\n        }\n        return $return;\n    }\n\n    \/**\n     * \u83b7\u53d6\u8868\u683c\u8282\u70b9\u5185\u5bb9\n     * @param $node\n     * @return string\n     *\/\n    public static function getTableNode($node)\n    {\n        $return = '';\n        \/\/\u5904\u7406\u884c\n        if ($node instanceof \\PhpOffice\\PhpWord\\Element\\Row) {\n            foreach ($node->getCells() as $ele)\n            {\n                $return .= self::getTableNode($ele);\n            }\n        }\n        \/\/\u5904\u7406\u5217\n        else if ($node instanceof \\PhpOffice\\PhpWord\\Element\\Cell) {\n            foreach ($node->getElements() as $ele)\n            {\n                $return .= self::getTextNode($ele);\n            }\n        }\n        return $return;\n    }\n\n    \/**\n     * \u5904\u7406word\u6587\u6863\u4e2dbase64\u683c\u5f0f\u56fe\u7247\n     * @param $node\n     * @return string\n     *\/\n    public static function pic2text($node)\n    {\n        \/\/\u83b7\u53d6\u56fe\u7247\u7f16\u7801\n        $imageData = $node->getImageStringData(true);\n        \/\/\u6dfb\u52a0\u56fe\u7247html\u663e\u793a\u6807\u5934\n        $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;\n        $return = '&lt;img src=\"'.$imageData.'\">';\n        return $return;\n    }\n    \/**\n     * \u5904\u7406word\u6587\u6863\u4e2dbase64\u683c\u5f0f\u56fe\u7247\n     * @param $node\n     * @return string\n     *\/\n    public static function pic2file($node)\n    {\n        \/\/\u56fe\u7247\u5730\u5740(\u4e00\u822c\u4e3aword\u6587\u6863\u5730\u5740+\u5728word\u4e2d\u7684\u951a\u70b9\u4f4d\u7f6e)\n        $imageSrc  = 'images\/' . md5($node->getSource()) . '.' . $node->getImageExtension();\n        $imageData = $node->getImageStringData(true);\n        \/\/\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u672c\u5730\n        file_put_contents($imageSrc, base64_decode($imageData));\n        return $imageSrc;\n    }\n\n    \/**\n     * \u5c06word\u8f6c\u5316\u4e3ahtml(\u8f6c\u6362\u5b58\u50a8html\u6587\u4ef6\u540e\u5c55\u793a)\n     * @param $path\n     * @throws \\PhpOffice\\PhpWord\\Exception\\Exception\n     *\/\n    public static function word2html($path)\n    {\n        $phpWord = FileImportService::getOne($path);\n        \/\/\u8f6c\u4e3ahtml\u5904\u7406\n        $xmlWriter  = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, \"HTML\");\n        $path = pathinfo($path);\n        $fileName = $path['dirname'] . '\/' . $path['filename'] . '.html';\n        $xmlWriter->save($fileName);\n        $html = file_get_contents($fileName);\n        echo $html;\n        die;\n\n    }\n\n\n}<\/code><\/pre>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>1. \u9996\u5148\u786e\u5b9a\u4f7f\u7528phpword\u662f\u53ef\u4ee5\u8bfb\u53d6word\u6587\u6863\u4e2d\u8868\u683c\u91cc\u9762\u7684\u6570\u636e, \u4f7f\u7528\u7684phpword\u7248\u672c0.17.0 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.yuekegu.com\/?p=240\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u4f7f\u7528phpword\u83b7\u53d6doc\u4e2d\u7684\u8868\u683c\u6570\u636e\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"post_thumbnail_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","content_first_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_medium_image_300":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_thumbnail_image_624":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_frist_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_medium_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_large_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_full_image":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","post_all_images":[{"imagesurl":"https:\/\/img2020.cnblogs.com\/blog\/1132687\/202010\/1132687-20201013164622599-2024844973.png","id":"image0"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":5,"total_comments":0,"category_name":"Yii2","post_date":"2021-03-09","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":false,"avatarurls":[],"related_posts":null,"pageviews":1357,"next_post_id":263,"next_post_title":"\u5173\u4e8e\u4e0a\u4e00\u9875\u4e0e\u4e0b\u4e00\u9875\u5b9e\u73b0\u7ec6\u8282","previous_post_id":238,"previous_post_title":"\u5173\u4e8eword\u6a21\u677f\u5bfc\u5165\u4e2dOLEObject\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u8f6c\u56fe\u7247\u7684\u65b9\u6cd5","_links":{"self":[{"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/posts\/240"}],"collection":[{"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":1,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yuekegu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}