PHP生成WORD

官方实例

function word(Request $request){
        //创建一个文档
        $phpWord = new \PhpOffice\PhpWord\PhpWord();

        /* Note: 添加到文档中的任何元素都必须在Section中 */

        // 添加新段落 换页
        $section = $phpWord->addSection();
        // 将文本元素添加到默认字体样式的段落
        $section->addText(
            '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'
        );

        /*
         * Note: 三种自定义样式方式
         * - 1. 内联;
         * - 2. 使用命名字体样式(将隐式创建新的字体样式对象);
         * - 3. 使用显式创建的字体样式对象.
         */

        // 内联样式
        //Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null)
        $section->addText(
            '"Great achievement is usually born of great sacrifice, '
            . 'and is never the result of selfishness." '
            . '(Napoleon Hill)',
            array('name' => 'Tahoma', 'size' => 10)
        );

        // 使用命名字体样式自定义字体
        $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);
        $fontStyle->setColor('1B2232');
        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
        $myTextElement->setFontStyle($fontStyle);

        // 将文档保存为OOXML文件
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('helloWorld.docx');

        // 将文档保存为ODF文件,写字板
        //$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
        //$objWriter->save('helloWorld.odt');

        // 将文档保存为HTML文件
        //$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
        //$objWriter->save('helloWorld.html');

}

实例2

function word(Request $request){

        //创建一个文档
        $phpWord = new \PhpOffice\PhpWord\PhpWord();

        //语言
        $language = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
        $phpWord->getSettings()->setThemeFontLang($language);

        //字体样式
        $fontStyleName = 'rStyle';
        $phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));

        //段落样式
        $paragraphStyleName = 'pStyle';
        //alignment 对齐方式
        $phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));

        $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));

        $section = $phpWord->addSection();
//        $section = $phpWord->addSection(
//            array(
//                'marginLeft'   => 200,
//                'marginRight'  => 200,
//                'marginTop'    => 200,
//                'marginBottom' => 200,
//                'headerHeight' => 50,
//                'footerHeight' => 50,
//            )
//        );

        $section->addTitle('addTitle', 1);
        $section->addText('addText addText addText addText');

        $section->addTextBreak(2);

        $section->addText('字体样式 I am styled by a font style definition.', $fontStyleName);
        $section->addText('段落样式 I am styled by a paragraph style definition.', null, $paragraphStyleName);
        $section->addText('字体样式 + 段落样式 I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);

        $section->addTextBreak();
        // 内联样式
        $fontStyle['name'] = 'Times New Roman';
        $fontStyle['size'] = 20;

        $textrun = $section->addTextRun();
        $textrun->addText('I am inline styled ', $fontStyle);
        $textrun->addText('with ');
        $textrun->addText('color', array('color' => '996699'));
        $textrun->addText(', ');
        $textrun->addText('bold', array('bold' => true)); //加粗
        $textrun->addText(', ');
        $textrun->addText('italic', array('italic' => true)); //斜体
        $textrun->addText(', ');
        $textrun->addText('underline', array('underline' => 'dash')); //下划线
        $textrun->addText(', ');
        $textrun->addText('strikethrough', array('strikethrough' => true)); //删除线
        $textrun->addText(', ');
        $textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));// 双删除线
        $textrun->addText(', ');
        $textrun->addText('superScript', array('superScript' => true)); //上标
        $textrun->addText(', ');
        $textrun->addText('subScript', array('subScript' => true)); //下标
        $textrun->addText(', ');
        $textrun->addText('smallCaps', array('smallCaps' => true)); //小型大写字母
        $textrun->addText(', ');
        $textrun->addText('allCaps', array('allCaps' => true)); //大写
        $textrun->addText(', ');
        $textrun->addText('fgColor', array('fgColor' => 'yellow')); //背景色
        $textrun->addText(', ');
        $textrun->addText('scale', array('scale' => 200)); //缩放
        $textrun->addText(', ');
        $textrun->addText('spacing', array('spacing' => 120)); //距离
        $textrun->addText(', ');
        $textrun->addText('kerning', array('kerning' => 10)); //字间距
        $textrun->addText('. ');

        // 链接
        $section->addLink('https://www.baidu.com', '百度');
        $section->addTextBreak();

        // 图片
        $section->addImage('logo.jpg', array('width'=>18, 'height'=>18));
        $section->addTextBreak();
        $section->addImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', array('width'=>18, 'height'=>18));

        //excel 文件关联
        //$textrun->addObject('excel.xls');

        // 底部
        //$footnote = $section->addFootnote();
        //$footnote->addText('底部');
        // 底部样式
        //$footnoteProperties = new \PhpOffice\PhpWord\ComplexType\FootnoteProperties();
        //$footnoteProperties->setNumFmt(\PhpOffice\PhpWord\SimpleType\NumberFormat::DECIMAL_ENCLOSED_CIRCLE);
        //$section->setFootnoteProperties($footnoteProperties);

        // 换页
        $section = $phpWord->addSection();

        // 定义样式 tab键距离
        $multipleTabsStyleName = 'multipleTab';
        $phpWord->addParagraphStyle(
            $multipleTabsStyleName,
            array(
                'tabs' => array(
                    new \PhpOffice\PhpWord\Style\Tab('left', 1550),
                    new \PhpOffice\PhpWord\Style\Tab('center', 3200),
                    new \PhpOffice\PhpWord\Style\Tab('right', 5300),
                ),
            )
        );

        $rightTabStyleName = 'rightTab';
        $phpWord->addParagraphStyle($rightTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090))));

        $leftTabStyleName = 'centerTab';
        $phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));

        // tab内容
        $section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, $multipleTabsStyleName);
        $section->addText("Left Aligned\tRight Aligned", null, $rightTabStyleName);
        $section->addText("\tCenter Aligned", null, $leftTabStyleName);

        // 换页
        $section = $phpWord->addSection();

        // 表格
        $rows = 10;
        $cols = 5;
        $table = $section->addTable();
        for ($r = 1; $r <= $rows; $r++) {
            $table->addRow();
            for ($c = 1; $c <= $cols; $c++) {
                $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
            }
        }

        $section->addTextBreak(1);

        // 表格样式
        $fancyTableStyleName = 'Fancy Table';
        $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50);
        $fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
        $fancyTableCellStyle = array('valign' => 'center'); //垂直居中
        $fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR); //textDirection文字方向
        $fancyTableFontStyle = array('bold' => true);
        $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
        $table = $section->addTable($fancyTableStyleName);
        $table->addRow(900);
        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 1', $fancyTableFontStyle);
        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 2', $fancyTableFontStyle);
        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 3', $fancyTableFontStyle);
        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 4', $fancyTableFontStyle);
        $table->addCell(500, $fancyTableCellBtlrStyle)->addText('Row 5', $fancyTableFontStyle);
        for ($i = 1; $i <= 8; $i++) {
            $table->addRow();
            $table->addCell(2000)->addText("Cell {$i}");
            $table->addCell(2000)->addText("Cell {$i}");
            $table->addCell(2000)->addText("Cell {$i}");
            $table->addCell(2000)->addText("Cell {$i}");
            $text = (0 == $i % 2) ? 'X' : '';
            $table->addCell(500)->addText($text);
        }

        // html
        $section = $phpWord->addSection();
        $html = '<h1>Adding element via HTML</h1>';
        $html .= '<p>Some well-formed HTML snippet needs to be used</p>';
        $html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';
        $html .= '<table align="center" style="width: 50%; border: 6px #0000FF double;">
                <thead>
                    <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
                        <th style="width: 50pt">header a</th>
                        <th style="width: 50">header          b</th>
                        <th style="background-color: #FFFF00; border-width: 12px"><span style="background-color: #00FF00;">header c</span></th>
                    </tr>
                </thead>
                <tbody>
                    <tr><td style="border-style: dotted; border-color: #FF0000">1</td><td colspan="2">2</td></tr>
                    <tr><td>This is <b>bold</b> text</td><td></td><td>6</td></tr>
                </tbody>
            </table>';

        \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

        // 将文档保存为OOXML文件
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('helloWorld2.docx');

表格

HTML

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值