>  기사  >  PHP 프레임워크  >  ThinkPHP6.0 콘텐츠 내보내기 Word 케이스 공유

ThinkPHP6.0 콘텐츠 내보내기 Word 케이스 공유

藏色散人
藏色散人앞으로
2020-12-22 09:19:592827검색

다음 thinkphpframework 튜토리얼 칼럼에서는 ThinkPHP6.0 콘텐츠 내보내기 Word 사례를 소개하겠습니다. 도움이 필요한 친구들에게 도움이 되길 바랍니다!

ThinkPHP6.0 콘텐츠 내보내기 Word 케이스 공유

(1) 환경 구성

  • 기본 환경
    • 시스템 환경: Windows10 x64
    • PHP 통합 환경: phpstudy
    • PHP 종속성 관리 도구: Composer
    • Manual: ThinkPHP

(2) ThinkPHP6.0 및 Phpword 확장 설치

(1) ThinkPHP6.0

composer create-project topthink/think phpword

설치(2) phpword 플러그인 설치

composer require phpoffice/phpword

(3) 콘텐츠를 word로 내보내기

(1) ) 콘텐츠를 내보내어 단어 문서를 생성합니다

<?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(&#39;orientation&#39; => null,
            &#39;marginLeft&#39; => 300,
            &#39;marginRight&#39; => 300,
            &#39;marginTop&#39; => 300,
            &#39;marginBottom&#39; => 400);
        $section = $phpWord->addSection($sectionStyle);
        //添加页眉
      /*  $header=$section->addHeader();
        $k=$header->addTextRun();
        //页眉添加一张图片
        $k->addImage(app()->getRootPath().&#39;public&#39;.DS."static/images/jrk.jpg",array(
            &#39;width&#39;         => &#39;100%&#39;,
            &#39;height&#39;        => 60,
            &#39;marginTop&#39;     => -1,
            &#39;marginLeft&#39;    => 1,
            &#39;wrappingStyle&#39; => &#39;behind&#39;,
        ));*/

        //添加页脚
        $footer = $section->addFooter();
        $f=$footer->addTextRun();

        $f->addImage(app()->getRootPath().&#39;public&#39;.DS."static/images/jrk.jpg",array(
            &#39;width&#39;         => 105,
            &#39;height&#39;        => 65,
            &#39;marginTop&#39;     => -1,
            &#39;marginLeft&#39;    => 1,
            &#39;wrappingStyle&#39; => &#39;behind&#39;,
        ));

        $footer->addPreserveText(&#39;Page {PAGE} of {NUMPAGES}.&#39;,array(&#39;align&#39;=>&#39;center&#39;));

        //添加标题
        if (!empty($title)){
            $section->addText(
                $title,
                array(&#39;name&#39; => &#39;黑体&#39;, &#39;size&#39; => 15),
                array(&#39;align&#39;=>&#39;center&#39;)
            );
        }
        //添加换行符
        $section->addTextBreak(2);

        //添加文本
        if (is_array($text)){
            foreach ($text as $v){
                $section->addText(
                    $v,
                    array(&#39;name&#39; => &#39;Arial&#39;, &#39;size&#39; => 13),
                    array(&#39;lineHeight&#39;=>1.5,&#39;indent&#39;=>1)
                );
            }
        }else{
            $section->addText(
                $text,
                array(&#39;name&#39; => &#39;Arial&#39;, &#39;size&#39; => 13),
                array(&#39;lineHeight&#39;=>1.5,&#39;indent&#39;=>1)
            );
        }
        $fname=Tool::uniqidDateCode();
        if ($save){
            /*保存文档到本地*/
            $objwrite =IOFactory::createWriter($phpWord);
            $t=date("Ymd",time());
            //保存的路径和中文名称适应
            $dir      = iconv("UTF-8", "GBK", app()->getRootPath().&#39;public&#39;.DS.&#39;words&#39;.DS.$t);
            if (!file_exists($dir)) {
                @mkdir($dir, 0777, true);
            }
            $pa = $t."/".$fname.".docx";
            $objwrite->save(app()->getRootPath().&#39;public&#39;.DS.&#39;phpoffices/words&#39;.DS.$pa);
            return  [&#39;code&#39;=>1,&#39;url&#39;=>&#39;/phpoffices/words/&#39;.$pa,&#39;domain&#39;=>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 = &#39;oneUserDefinedStyle&#39;;
        $phpWord->addFontStyle(
            $fontStyleName,
            array(&#39;name&#39; => &#39;Tahoma&#39;, &#39;size&#39; => 13, &#39;color&#39; => &#39;1B2232&#39;, &#39;bold&#39; => 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, &#39;HTML&#39;);
            $t=date("Ymd",time());
            //保存的路径和中文名称适应
            $dir      = iconv("UTF-8", "GBK", app()->getRootPath().&#39;public&#39;.DS.&#39;phpoffices/htmls&#39;.DS.$t);
            if (!file_exists($dir)) {
                @mkdir($dir, 0777, true);
            }
            $pa = $t."/".$fname.".html";
            $objwrite->save(app()->getRootPath().&#39;public&#39;.DS.&#39;phpoffices/htmls&#39;.DS.$pa);
            return  [&#39;code&#39;=>1,&#39;url&#39;=>&#39;/phpoffices/htmls/&#39;.$pa,&#39;domain&#39;=>request()->domain(true)];
        }else{
            $name=$fname.".html"; //文件名称
            $phpWord->save($name,"HTML",true);
        }
        exit;
    }

위 내용은 ThinkPHP6.0 콘텐츠 내보내기 Word 케이스 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 learnku.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제