您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關ThinkPHP6.0內容導 Word的使用方法的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
(一)環境配置
基礎環境
系統環境:Windows10 x64
PHP集成環境:phpstudy
PHP依賴管理工具:Composer
手冊:Thinkphp
(二)安裝ThinkPHP6.0和Phpword擴展
(1)安裝ThinkPHP6.0
composer create-project topthink/think phpword
(2)安裝phpword插件
composer require phpoffice/phpword
(三) 內容導出word
(1)內容導出生成word文檔
<?php namespace app\admin\service; use Jrk\Tool; use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\PhpWord; class WordService { /** * @param $text * @param null $title * @param bool $save * @return array * @throws \PhpOffice\PhpWord\Exception\Exception * @author: LuckyHhy <jackhhy520@qq.com> * @describe: */ public static function exportToword($text,$title=null,$save=false){ $phpWord=new PhpWord(); //實例化 //調整頁面樣式 $sectionStyle = array('orientation' => null, 'marginLeft' => 300, 'marginRight' => 300, 'marginTop' => 300, 'marginBottom' => 400); $section = $phpWord->addSection($sectionStyle); //添加頁眉 /* $header=$section->addHeader(); $k=$header->addTextRun(); //頁眉添加一張圖片 $k->addImage(app()->getRootPath().'public'.DS."static/images/jrk.jpg",array( 'width' => '100%', 'height' => 60, 'marginTop' => -1, 'marginLeft' => 1, 'wrappingStyle' => 'behind', ));*/ //添加頁腳 $footer = $section->addFooter(); $f=$footer->addTextRun(); $f->addImage(app()->getRootPath().'public'.DS."static/images/jrk.jpg",array( 'width' => 105, 'height' => 65, 'marginTop' => -1, 'marginLeft' => 1, 'wrappingStyle' => 'behind', )); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.',array('align'=>'center')); //添加標題 if (!empty($title)){ $section->addText( $title, array('name' => '黑體', 'size' => 15), array('align'=>'center') ); } //添加換行符 $section->addTextBreak(2); //添加文本 if (is_array($text)){ foreach ($text as $v){ $section->addText( $v, array('name' => 'Arial', 'size' => 13), array('lineHeight'=>1.5,'indent'=>1) ); } }else{ $section->addText( $text, array('name' => 'Arial', 'size' => 13), array('lineHeight'=>1.5,'indent'=>1) ); } $fname=Tool::uniqidDateCode(); if ($save){ /*保存文檔到本地*/ $objwrite =IOFactory::createWriter($phpWord); $t=date("Ymd",time()); //保存的路徑和中文名稱適應 $dir = iconv("UTF-8", "GBK", app()->getRootPath().'public'.DS.'words'.DS.$t); if (!file_exists($dir)) { @mkdir($dir, 0777, true); } $pa = $t."/".$fname.".docx"; $objwrite->save(app()->getRootPath().'public'.DS.'phpoffices/words'.DS.$pa); return ['code'=>1,'url'=>'/phpoffices/words/'.$pa,'domain'=>request()->domain(true)]; }else{ //不保存到服務器,直接輸出瀏覽器下載 $name=$fname.".docx"; //文件名稱 $phpWord->save($name,"Word2007",true); } exit; } }
(2)內容生成 html 文件
/** * @param $text * @param bool $save * @return array * @throws \PhpOffice\PhpWord\Exception\Exception * @author: LuckyHhy <jackhhy520@qq.com> * @describe: */ public static function makeHtml($text,$save=false){ $phpWord=new PhpWord(); //實例化 $section = $phpWord->addSection(); $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' => 'Tahoma', 'size' => 13, 'color' => '1B2232', 'bold' => true) ); if (is_array($text)){ foreach ($text as $v){ $section->addText( $v, $fontStyleName ); } }else{ $section->addText( $text, $fontStyleName ); } $fname=Tool::uniqidDateCode(); if ($save){ $objwrite = IOFactory::createWriter($phpWord, 'HTML'); $t=date("Ymd",time()); //保存的路徑和中文名稱適應 $dir = iconv("UTF-8", "GBK", app()->getRootPath().'public'.DS.'phpoffices/htmls'.DS.$t); if (!file_exists($dir)) { @mkdir($dir, 0777, true); } $pa = $t."/".$fname.".html"; $objwrite->save(app()->getRootPath().'public'.DS.'phpoffices/htmls'.DS.$pa); return ['code'=>1,'url'=>'/phpoffices/htmls/'.$pa,'domain'=>request()->domain(true)]; }else{ $name=$fname.".html"; //文件名稱 $phpWord->save($name,"HTML",true); } exit; }
感謝各位的閱讀!關于ThinkPHP6.0內容導 Word的使用方法就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。