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

 

PHPWord Beta 0.6.2 开发者指南 目 录 首先我们要了解文档最基本的信息和设置: 4 计量单位:缇(twips) 4 字体设置 4 文档属性设置 4 新建文档 5 添加页面 5 页面样式 5 页面样式属性 6 文本 7 添加文本 7 添加文本资源 7 文本样式 8 样式属性列表 9 添加换行符 10 添加分页符 10 列表 10 添加列表 10 列表样式 11 列表样式属性列表 11 超链接 11 添加超链接 11 超链接样式 12 图片 13 添加图片 13 图片样式 13 图片样式属性 13 添加GD生成图片 14 添加水印 14 添加对象 15 添加标题 15 添加目录 16 表格 17 添加表格 17 添加行 17 添加单元格 17 单元格样式 19 表格样式 20 页脚 22 页眉 23 模版 23 其他问题修改 25 解决文本缩进问题 25 表格对齐和表格缩进 27 图片缩进和绝对相对悬浮定位 30 首先我们要了解文档最基本的信息和设置:  因为是国外编辑的类库,存在对中文支持的问题,使用前,我们需要进行一些修正: 1、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。 2、解决中文字体支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font) 3、启动php zip支持,windows环境下在php配置文件php.ini中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip.dll文件存在相应的目录),然后同样在php.ini文件中,将 zlib.output_compression = Off 改为zlib.output_compression = On ; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值