ThinkPHP6.0 內容匯出 Word 案例

TechKoga發表於2020-08-17

(1)安裝ThinkPHP6.0

composer create-project topthink/think phpword

(2)安裝phpword外掛

composer require phpoffice/phpword

(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;
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結
愛程式碼,不愛程式設計的小夥子 ^v^

相關文章