创建 Word

文档

文档:https://phpoffice.github.io/PHPWord/
github:https://github.com/PHPOffice/PHPWord

安装

composer require phpoffice/phpword

使用

use zxf\Office\Word\Word;


$imgPath  = '/Users/linian/Pictures/1.jpeg';
$savePath = '/Users/linian/Desktop/document_write.docx';
// 创建文档并添加内容

$filePath = '/Users/linian/Desktop/document1.docx';
// 创建 Write 类的实例,传入$filePath就会加载$filePath文件,不传就新建一个对象
$write = new Word($filePath);

// 添加标题
$write->setTitle('文档标题', [
    'name'  => 'Arial',
    'size'  => 16,
    'bold'  => true,
    'color' => '0000FF',
]);

$write->addText('加粗文字:', ['bold' => true]);

// 添加文本
$write->addText('这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本。', [
    'name'  => 'Arial',
    'size'  => 12,
    'color' => '000000',
], [
    'align'       => 'left',
    'spaceBefore' => 10,
    'spaceAfter'  => 10,
]);

// 添加带下划线的文本
$write->addTextWithUnderline('这是带下划线的文本。', [
    'name'  => 'Arial',
    'size'  => 12,
    'color' => 'FF0000',
]);
$write->addText('这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本这是普通文本。', [
    'name'  => 'Arial',
    'size'  => 12,
    'color' => '000000',
]);

// 替换文本
$write->replaceText('普通文本', '替换后的文本');

// 添加有序列表
$write->addOrderedList([
    '第一项',
    '第二项',
    '第三项',
]);

// 添加无序列表
$write->addUnorderedList([
    '第一项',
    '第二项',
    '第三项',
]);

// 添加表格
$write->addTable('center', function ($table) use ($imgPath) {
    $table->addRow(400)
        ->addColSpanCell(2000, '跨两列的单元格', 2) // 添加跨两列的单元格
        ->addCell(1000, '右侧单元格')  // 普通单元格
        ->addRow()
        ->addCell(1000, '单元格 1')  // 普通单元格
        ->addCell(1000, '单元格 2')  // 普通单元格
        ->addCell(1000, '单元格 3')  // 普通单元格
        ->addRow()
        ->addRowSpanCell(1000, '跨两行', 2) // 添加跨两行的单元格
        ->addImageCell(1000, $imgPath, [
            'width'     => 50,
            'height'    => 50,
            'alignment' => 'center',
        ])
        ->addRowSpanCell(1000, '单元格 4', 2)
        ->addRow()
        ->addCell(1000, '单元格 5')
        ->addRow()
        ->addCell(1000, '单元格 6')
        ->addCell(1000, '单元格 x')
        ->addCell(1000, '单元格 x-1')
        ->addRow()
        ->addCell(1000, '单元格 7')
        ->addColSpanCell(2000, '单元格 8', 2) // 跨列单元格
    ;
});

// 添加统一的页眉
$write->addHeader('这是页眉内容');

// 添加统一的页脚
$write->addFooter('这是页脚内容');

// 或者调用自定义闭包回调操作 奇偶页页眉
$write->customCall(function ($phpWord, $section) {
    // 开启设置 奇偶不同的页眉
    $phpWord->getSettings()->setEvenAndOddHeaders(true);

    $style = array('alignment' => 'center'); // 设置页眉内容居中对齐

    // 默认页和奇数页
    $headerOdd = $section->addHeader();
    $headerOdd->addText('这是奇数页页眉', null, $style);
    // 偶数页页眉
    $headerEven = $section->addHeader('even');
    $headerEven->addText('这是偶数页页眉', null, $style);

    // 页脚和页眉设置相同
});

// 插入页码
$write->addPageNumber('center');

// 本页设置为两列
$write->setColsNum(2);
        
// 设置文本背景色
$write->setTextBackgroundColor('#FFFF00');

$write->addText('这是文本这是文本这是文本这是文本');
$write->addBr(3);
$write->addText('这是文本这是文本这是文本这是文本');


// 统一本页段落间距
$write->setParagraphSpacing(90, 90, 40);

$write->addPaper();
$write->addText('这是文本这是文本这是文本这是文本');

$write->addLink('http://0l0.net', '测试链接');

// 添加图片
$write->addImage($imgPath, [
    'width'  => 200,
    'height' => 150,
    'align'  => 'center',
]);

// 调用PHPWord的其他操作
// 回调参数
// $phpWord: 当前文档对象
// $section: 当前页文档
// $word: 当前Word类
$write->customCall(function ($phpWord, $section, $word) {
    // 添加折线图
    $categories = array('A', 'B', 'C', 'D', 'E');
    $series     = array(1, 3, 2, 5, 4);
    // https://phpoffice.github.io/PHPWord/usage/styles/chart.html
    $section->addChart('line', $categories, $series, [
            'width'             => $word->cmToEmu(8), // 8 cm,只能使用EMU 单位
            'height'            => $word->cmToEmu(6), // 6 cm,只能使用EMU 单位
            '3d'                => false,
            'title'             => '折线图',
            'showLegend'        => false,
            'gridX'             => true, // 显示Y网格
            'gridY'             => true, // 显示Y网格
            'showAxisLabels'    => true,
            'categoryAxisTitle' => 'xxx', // X 轴标题
            'valueAxisTitle'    => 'yyy', // Y 轴标题
        ]
    );

    // 添加CheckBox
    $section->addCheckBox('CheckBox Name', 'CheckBox Text', [
        'color'   => 'FF0000',
        'bgColor' => '00FF00',
        'bold'    => false,
        'size'    => 22,
    ], []);

});

// 统一本页段落间距
$write->setParagraphSpacing(90, 90, 40);

// 保存文档到文件
$write->save($savePath);

// 直接下载文档
$write->download('example_download.docx');