phpword表格使用以及文字居中、单元格合并问题

 注:在thinkphp6.0项目目录使用composer下载phpoffice,以下是官方给的table的案例

<?php
namespace app\controller;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Style\TablePosition;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Cell;
use PhpOffice\PhpWord\SimpleType\Jc;

class Test
{
    public function word()
    {
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        $header = array('size' => 16, 'bold' => true);

        // 1. Basic table

        $rows = 10;
        $cols = 5;
        $section->addText('Basic table', $header);

        $table = $section->addTable();
        for ($r = 1; $r <= 8; $r++) {
            $table->addRow();
            for ($c = 1; $c <= 5; $c++) {
                $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
            }
        }

        // 2. Advanced table

        $section->addTextBreak(1);
        $section->addText('Fancy table', $header);

        $fancyTableStyleName = 'Fancy Table';
        $fancyTableStyle = array(
            'borderSize' => 6,
            'borderColor' => '006699',
            'cellMargin' => 80,
            'alignment' => JcTable::CENTER,
            'cellSpacing' => 50
        );
        $fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
        $fancyTableCellStyle = array('valign' => 'center');
        $fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => Cell::TEXT_DIR_BTLR);
        $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);
        }

        /*
         *  3. colspan (gridSpan) and rowspan (vMerge)
         *  ---------------------
         *  |     |   B    |    |
         *  |  A  |--------|  E |
         *  |     | C |  D |    |
         *  ---------------------
         */

        $section->addPageBreak();
        $section->addText('Table with colspan and rowspan', $header);

        $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999');
        $cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center', 'bgColor' => 'FFFF00');
        $cellRowContinue = array('vMerge' => 'continue');
        $cellColSpan = array('gridSpan' => 2, 'valign' => 'center'); // 单元格列合并
        $cellHCentered = array('alignment' => Jc::CENTER); // 水平居中
        $cellVCentered = array('valign' => 'center'); // 垂直居中

        $spanTableStyleName = 'Colspan Rowspan';
        $phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
        $table = $section->addTable($spanTableStyleName);

        $table->addRow();

        $cell1 = $table->addCell(2000, $cellRowSpan);
        $textrun1 = $cell1->addTextRun($cellHCentered);
        $textrun1->addText('A');
        $textrun1->addFootnote()->addText('Row span');

        $cell2 = $table->addCell(4000, $cellColSpan);
        $textrun2 = $cell2->addTextRun($cellHCentered);
        $textrun2->addText('B');
        $textrun2->addFootnote()->addText('Column span');

        $table->addCell(2000, $cellRowSpan)->addText('E', null, $cellHCentered);

        $table->addRow();
        $table->addCell(null, $cellRowContinue);
        $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered);
        $table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered);
        $table->addCell(null, $cellRowContinue);

        /*
         *  4. colspan (gridSpan) and rowspan (vMerge)
         *  ---------------------
         *  |     |   B    |  1 |
         *  |  A  |        |----|
         *  |     |        |  2 |
         *  |     |---|----|----|
         *  |     | C |  D |  3 |
         *  ---------------------
         * @see https://github.com/PHPOffice/PHPWord/issues/806
         */

        $section->addPageBreak();
        $section->addText('Table with colspan and rowspan', $header);

        $styleTable = array('borderSize' => 6, 'borderColor' => '999999');
        $phpWord->addTableStyle('Colspan Rowspan', $styleTable);
        $table = $section->addTable('Colspan Rowspan');

        $row = $table->addRow();
        $row->addCell(1000, array('vMerge' => 'restart'))->addText('A');
        $row->addCell(1000, array('gridSpan' => 2, 'vMerge' => 'restart'))->addText('B');
        $row->addCell(1000)->addText('1');

        $row = $table->addRow();
        $row->addCell(1000, array('vMerge' => 'continue'));
        $row->addCell(1000, array('vMerge' => 'continue', 'gridSpan' => 2));
        $row->addCell(1000)->addText('2');

        $row = $table->addRow();
        $row->addCell(1000, array('vMerge' => 'continue'));
        $row->addCell(1000)->addText('C');
        $row->addCell(1000)->addText('D');
        $row->addCell(1000)->addText('3');

        // 5. Nested table
        $section->addTextBreak(2);
        $section->addText('Nested table in a centered and 50% width table.', $header);
        $table = $section->addTable(
            array(
                'width' => 50 * 50,
                'unit' => 'pct',
                'alignment' => JcTable::CENTER
            )
        );
        $cell = $table->addRow()->addCell();
        $cell->addText('This cell contains nested table.');
        $innerCell = $cell->addTable(array('alignment' => JcTable::CENTER))->addRow()->addCell();
        $innerCell->addText('Inside nested table');

        // 6. Table with floating position
        $section->addTextBreak(2);
        $section->addText('Table with floating positioning.', $header);
        $table = $section->addTable(
            array(
                'borderSize' => 6,
                'borderColor' => '999999',
                'position' => array(
                    'vertAnchor' => TablePosition::VANCHOR_TEXT,
                    'bottomFromText' => Converter::cmToTwip(1))
            )
        );
        $cell = $table->addRow()->addCell();
        $cell->addText('This is a single cell.');

        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('test.docx');
    }
}

运行导出word文档,显示内容如下:

 

说明:看官方文档没找到表格内容居中,在官方给的示例中找到了相关代码。

详见:https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_09_Tables.php

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值