分享按钮

TP5 解决如何实现生成并导出Word文档功能

默认分类 / 7455人浏览 / 0人评论

1.下载安装phpword插件:composer require phpoffice/phpword




// test

    public function test(){

        $PHPWord = new PhpWord();

 header("Content-Type: text/html; charset=UTF-8");

 

        $PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 12));//定义样式

        $PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));

        $PHPWord->addTitleStyle('titlestyle', array('bold' => true,'size' => 16),array('spaceAfter' => 240,'align' => 'center'));//标题的样式


        // New portrait section

        $section = $PHPWord->createSection();

        $arr['project_name'] = '云桥';

        $arr['buy_start_time'] = '20180515';

        $arr['buy_end_time'] = '20190825';

        $arr['start_s'] = '20190825';

        $arr['end_s'] = '20190825';

        $arr['total'] = 25;

        $arr['tfee'] = 2500;

        // Add text elements

        $str = "        ".$arr['project_name']."项目,与腾讯房产于".$arr['buy_start_time']."至".$arr['buy_end_time']."开展腾讯电商团购合作,".$arr['start_s']."至".$arr['end_s']."内,共计售出房屋". $arr['total']."套,成交明细见附件,收取服务费合计".$arr['tfee']."元,特此证明。";

        $str5 = "        本确认函中的房源均已通过网签为确认标准,经双方授权代表签字后生效作为收款确认依据。且一旦签字盖章,乙方将不再承担该房源后续的电商团购费的退款责任。";

        $str1 = "甲    方:                                                           乙    方:";

        $str2 = "授权代表签字:                                                 授权代表签字:";

        $str3 = "盖章:                                                                  盖章:";

 

        $str4 = "签约时间:20        年        月        日                                    签约时间:20         年        月        日";

        $title = '<h1>腾讯电商合作成交签约确认函</h1>';

        $section->addText($title,'rStyle','pStyle');

        $section->addTextBreak(2);

        $section->addText($str,'cOntent');

        $section->addTextBreak(2);

        $section->addText($str5,'cOntent');

        $section->addTextBreak(2);

        // $section->addText(iconv('utf-8','GB2312//IGNORE',$str1),'cOntent');

        // $section->addText(iconv('utf-8','GB2312//IGNORE',$str2),'cOntent');

        // $section->addText(iconv('utf-8','GB2312//IGNORE',$str3),'cOntent');

        // $section->addText(iconv('utf-8','GB2312//IGNORE',$str4),'cOntent');

        $section->addText($str1,'cOntent');

        $section->addText($str2,'cOntent');

        $section->addText($str3,'cOntent');

        $section->addText($str4,'cOntent');

        $section->addTextBreak(2);

//        $section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));

        $section->addTextBreak(2);

        $PHPWord->addFontStyle('cOntent', array('bold'=>false, 'size'=>12));

        $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>false, 'size'=>16,'align'=>'center'));

        $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));

//        $section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');

//        $section->addText('I have only a paragraph style definition.', null, 'pStyle');

//         Save File

        $objWriter = IOFactory::createWriter($PHPWord, 'Word2007');

        /*header("Content-Type: application/doc");

        header("Content-Disposition: attachment; filename=".date("YmdHis").".doc");*/

        $path = './report/'.date("YmdHis").'.doc';

        $objWriter->save($path);

        $file1 = fopen($path, "r");

        // 输入文件标签

        Header("Content-type: application/octet-stream");

        Header("Accept-Ranges: bytes");

        Header("Accept-Length:".filesize($path));

        Header("Content-Disposition: attachment;filename=" . date("YmdHis").'.doc');

        ob_clean();     // 重点!!!

        flush();        // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题:

        //输出文件内容

        //读取文件内容并直接输出到浏览器

        echo fread($file1, filesize($path));

        fclose($file1);

        exit();

    }



感谢博主,喝杯咖啡~