您现在的位置是: 首页 >  PHP PHP

ThinkPHP6.0 内容导出 Word 案例

冬寂 2020-08-18 09:02:04 【PHP】 1890人已围观

(一)环境配置

- 基础环境

(二)安装 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;
    }
很赞哦! ( 7 )

站点信息

  • 建站时间:2020-08-10
  • 文章统计100篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我