PHP、TP5生成下载word

我觉得用composer安装比较好,免得引起一些不必要的麻烦,composer 会自动建立好依赖关系。

首先windows要先安装composer。不懂百度。。。

通常第三方库 都是安装在vendor目录下 

 

开始操作:

在vendor目录 同级  

新建composer.json文件

{
  "comment" : "以下是PHPWord包",
  "require": {
    "phpoffice/phpword": "v0.14.*",
    "ext-xml": "*",
    "zendframework/zend-escaper": "^2.2"
  }
}

如图:

PHP、TP5生成下载word_第1张图片

我是把vendor定义到统一和extend同级  都在core 文件夹下,个人喜好。

cmd 切换到composer.json目录     composer install 

PHP、TP5生成下载word_第2张图片

PHP、TP5生成下载word_第3张图片

下载完成

PHP、TP5生成下载word_第4张图片

 

测试代码:

    public function index(){
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        $section->addText(
            '我想做一个测试看看是否成功!'
        );
        $section->addText(
            '"Great achievement is usually born of great sacrifice, '
            . 'and is never the result of selfishness." '
            . '(Napoleon Hill)',
            array('name' => 'Tahoma', 'size' => 40)
        );
        $fontStyleName = 'oneUserDefinedStyle';
        $phpWord->addFontStyle(
            $fontStyleName,
            array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
        );
        $section->addText(
            '"The greatest accomplishment is not in never falling, '
            . 'but in rising again after you fall." '
            . '(Vince Lombardi)',
            $fontStyleName
        );

        //设置字体字号等
        $fontStyle = new \PhpOffice\PhpWord\Style\Font();
        $fontStyle->setBold(true);
        $fontStyle->setName('Tahoma');
        $fontStyle->setSize(13);
        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
        $myTextElement->setFontStyle($fontStyle);


        if(!file_exists(iconv('utf-8', 'gbk', 'D:\\报告')))
            mkdir(iconv('utf-8', 'gbk', 'D:\\报告'));
        $time = date('YmdHis',time());
        $name = iconv('utf-8', 'gbk','D:\\报告\\'.$time . '分析报告.docx');

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save($name);


//        //生成odt
//        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
//        $objWriter->save('D:\testWord/'.$name.'.odt');
//
//        //生成html
//        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
//        \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
//
//        $objWriter->save('D:\testWord/'.$name.'.html');


        echo  '已经生成word文档!';
        exit;

    }

PHP、TP5生成下载word_第5张图片

PHP、TP5生成下载word_第6张图片

测试完成!  

你可能感兴趣的:(php)