{"id":916,"date":"2023-01-17T14:06:22","date_gmt":"2023-01-17T06:06:22","guid":{"rendered":"https:\/\/site.uugo.cf\/?p=916"},"modified":"2023-01-17T16:24:20","modified_gmt":"2023-01-17T08:24:20","slug":"phpspreadsheet","status":"publish","type":"post","link":"https:\/\/site.uugo.cf\/?p=916","title":{"rendered":"PHPSpreadsheet\u548cPHPWord"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>PhpSpreadsheet\u662f\u4e00\u4e2a\u7eafPHP\u7c7b\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7c7b\uff0c\u5141\u8bb8\u60a8\u4ece\u4e0d\u540c\u7684\u7535\u5b50\u8868\u683c\u6587\u4ef6\u683c\u5f0f\uff08\u5982Excel\u548cLibreOffice Calc\uff09\u8bfb\u53d6\u548c\u5199\u5165\u3002\u7528PHP\u8bfb\u53d6Excel\u3001CSV\u6587\u4ef6<\/li>\n\n\n\n<li>\u8fd8\u6709\u4e00\u79cd\u7c7b\u4f3c\u7684\u7c7b\u5e93PHPExcel\uff0c\u4f46\u662fPhpSpreadsheet\u4ee3\u7801\u8d28\u91cf\u548c\u6027\u80fd\u6bd4PHPExcel\u9ad8\u4e0d\u5c11\uff0c\u5b8c\u5168\u53ef\u4ee5\u66ff\u4ee3PHPExcel\uff08PHPExcel\u5df2\u4e0d\u518d\u7ef4\u62a4\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PhpSpreadsheet\u5b89\u88c5\u7684\u73af\u5883\u8981\u6c42\uff1a<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>PHP version 7.4\u6216\u8005\u66f4\u9ad8<\/li>\n\n\n\n<li>PHP extension\uff1aphp_zip enabled\uff0cphp_xml enabled\uff0cphp_gd2 enabled<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PhpSpreadsheet\u7ec4\u4ef6\u5b89\u88c5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528composer\u6267\u884c\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\uff1a<code> composer require phpoffice\/phpspreadsheet &lt;\u7248\u672c\u53f7&gt;<\/code><\/li>\n\n\n\n<li>\u6267\u884c\u5b8c\u547d\u4ee4\u540e\u4f1a\u5728vendor\u76ee\u5f55\u4e0b\u53d1\u73b0\u6709\u4e86phpoffice\u76ee\u5f55\uff0c\u4ee3\u8868\u5df2\u7ecf\u4e0b\u8f7d\u5b89\u88c5PhpSpreadsheet\u6210\u529f\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PhpSpreadsheet\u7b80\u5355\u5b9e\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire 'vendor\/autoload.php';\n\nuse PhpOffice\\PhpSpreadsheet\\Spreadsheet;\nuse PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx;\n\n$spreadsheet = new Spreadsheet();\n$sheet = $spreadsheet-&gt;getActiveSheet();\n$sheet-&gt;setCellValue('A1', 'Hello World !');\n\n$writer = new Xlsx($spreadsheet);\n$writer-&gt;save('hello world.xlsx');\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u5b98\u65b9\u5e2e\u52a9\u7f51\u7ad9\uff1a <a rel=\"noreferrer noopener\" href=\"https:\/\/phpspreadsheet.readthedocs.io\/en\/latest\/\" target=\"_blank\">https:\/\/phpspreadsheet.readthedocs.io\/en\/latest\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Phpword\u7ec4\u4ef6\u5b89\u88c5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528composer\u6267\u884c\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\uff1a<code> composer require phpoffice\/phpword &lt;\u7248\u672c\u53f7&gt;<\/code><\/li>\n\n\n\n<li>\u6267\u884c\u5b8c\u547d\u4ee4\u540e\u4f1a\u5728vendor\u76ee\u5f55\u4e0b\u53d1\u73b0\u6709\u4e86phpoffice\u76ee\u5f55\uff0c\u4ee3\u8868\u5df2\u7ecf\u4e0b\u8f7d\u5b89\u88c5Phpword\u6210\u529f\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PhpWord\u7b80\u5355\u5b9e\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nrequire 'vendor\/autoload.php';\n\n\/\/ Creating the new document...\nuse PhpOffice\\PhpWord\\Settings;\n$phpWord = new \\PhpOffice\\PhpWord\\PhpWord();\n\n\/* Note: any element you append to a document must reside inside of a Section. *\/\n\n\/\/ Adding an empty Section to the document...\n$section = $phpWord-&gt;addSection();\n\/\/ Adding Text element to the Section having font styled by default...\n$section-&gt;addText(\n    '\"Learn from yesterday, live for today, hope for tomorrow. '\n        . 'The important thing is not to stop questioning.\" '\n        . '(Albert Einstein)'\n);\n\n\/*\n * Note: it's possible to customize font style of the Text element you add in three ways:\n * - inline;\n * - using named font style (new font style object will be implicitly created);\n * - using explicitly created font style object.\n *\/\n\n\/\/ Adding Text element with font customized inline...\n$section-&gt;addText(\n    '\"Great achievement is usually born of great sacrifice, '\n        . 'and is never the result of selfishness.\" '\n        . '(Napoleon Hill)',\n    array('name' =&gt; 'Tahoma', 'size' =&gt; 10)\n);\n\n\/\/ Adding Text element with font customized using named font style...\n$fontStyleName = 'oneUserDefinedStyle';\n$phpWord-&gt;addFontStyle(\n    $fontStyleName,\n    array('name' =&gt; 'Tahoma', 'size' =&gt; 10, 'color' =&gt; '1B2232', 'bold' =&gt; true)\n);\n$section-&gt;addText(\n    '\"The greatest accomplishment is not in never falling, '\n        . 'but in rising again after you fall.\" '\n        . '(Vince Lombardi)',\n    $fontStyleName\n);\n\n\/\/ Adding Text element with font customized using explicitly created font style object...\n$fontStyle = new \\PhpOffice\\PhpWord\\Style\\Font();\n$fontStyle-&gt;setBold(true);\n$fontStyle-&gt;setName('Tahoma');\n$fontStyle-&gt;setSize(13);\n$myTextElement = $section-&gt;addText('\"Believe you can and you\\'re halfway there.\" (Theodor Roosevelt)');\n$myTextElement-&gt;setFontStyle($fontStyle);\n\n\/\/ Saving the document as OOXML file...\n$objWriter = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, 'Word2007');\n$objWriter-&gt;save('helloWorld.docx');\n\n\/\/ Saving the document as ODF file...\n$objWriter = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, 'ODText');\n$objWriter-&gt;save('helloWorld.odt');\n\n\/\/ Saving the document as HTML file...\n$objWriter = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, 'HTML');\n$objWriter-&gt;save('helloWorld.html');\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PHPword\u8bfb\u53d6word\u6587\u4ef6\u4ee3\u7801\u6837\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n * Created by PhpStorm.\n * User: chengyanping\n * Date: 2021\/5\/19\n * Time: 13:14\n *\/\nrequire_once '.\/vendor\/autoload.php';\nini_set('display_errors', true);\nini_set('max_execution_time', '0');\nerror_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);\n \nnamespace common\\services;\n \nclass WordService extends BaseService\n{\n \n    public static function importWord($info)\n    {\n        $word  = self::getWord($info&#91;'path']);\n\/\/      print_r($word);\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\/\/   print_R($phpWord);exit();\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 = &#91;];\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&#91;] = $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&#91;] = 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&#91;'dirname'] . '\/' . $path&#91;'filename'] . '.html';\n        $xmlWriter->save($fileName);\n        $html = file_get_contents($fileName);\n        echo $html;\n        die;\n \n    }\n \n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PhpSpreadsheet\u5b89\u88c5\u7684\u73af\u5883\u8981\u6c42\uff1a PhpSpreadsheet\u7ec4\u4ef6\u5b89\u88c5 PhpSpreadsheet\u7b80\u5355\u5b9e\u4f8b \u5b98\u65b9\u5e2e\u52a9\u7f51\u7ad9\uff1a https:\/\/phpspreadsheet.readthedocs.io\/en\/latest\/ Ph&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-916","post","type-post","status-publish","format-standard","hentry","category-7"],"views":254,"_links":{"self":[{"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/posts\/916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=916"}],"version-history":[{"count":4,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/posts\/916\/revisions"}],"predecessor-version":[{"id":924,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=\/wp\/v2\/posts\/916\/revisions\/924"}],"wp:attachment":[{"href":"https:\/\/site.uugo.cf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/site.uugo.cf\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}