php 使用phpoffice/phpword导出word

发布时间 2023-07-05 10:57:41作者: 雨过了天晴

安装

 composer require phpoffice/phpword   
    /**
     *  // 设置常用文本样式
     * 'size'   => 12,    // 文字大小
     * 'name'   => '宋体', // 字体名称
     * 'bold'   => true,  // 加粗
     * 'italic' => true,  // 斜体
     * 'color'  => 'red', // 颜色
     *
     *  // 设置常用页面样式
     * 'orientation'       => null,                 // 页面方向,默认null是竖向,landscape是横向
     * 'indentation'       => ['firstLine' => 600], // 缩进
     * 'align'             => 'center',             // 对齐方式
     * 'marginTop'         => 800,                  // 上边距
     * 'marginLeft'        => 800,                  // 左边距
     * 'marginRight'       => 800,                  // 右边距
     * 'marginBottom'      => 800,                  // 下边距
     * 'borderTopSize'     => 800,                  // 上边框尺寸
     * 'borderTopColor'    => 'red',                // 上边框颜色
     * 'borderLeftSize'    => 800,                  // 左边框尺寸
     * 'borderLeftColor'   => 'red',                // 左边框颜色
     * 'borderRightSize'   => 800,                  // 右边框尺寸
     * 'borderRightColor'  => 'red',                // 右边框颜色
     * 'borderBottomSize'  => 800,                  // 下边框尺寸
     * 'borderBottomColor' => 'red',                // 下边框颜色
     */

 

设置页眉

$phpWord = new PhpWord();
$section = $phpWord->addSection(); // 新增一个空白页

// 创建带有格式的标题
$header = $section->addHeader();
$table  = $header->addTable();
$table->addRow();
$cell = $table->addCell(9000); // 调整单元格宽度以适应标题内容
$cell->addText('我是页眉', ['size' => 22, 'name' => '方正小标宋_GBK'], ['align' => 'center']);

插入图片

// 插入图片,宽200像素,高200像素
$section->addImage('path/to/your/image.jpg', ['width' => 200, 'height' => 200]);

// 调整图片位置和大小(可选)
$image->setAlignment(\PhpOffice\PhpWord\SimpleType\Jc::CENTER); // 设置图片居中对齐 
$image->setWidth(300); // 设置图片宽度,单位为像素 
$image->setHeight(300); // 设置图片高度,单位为像素

 

插入文字

// 添加带有首行缩进的段落
$text1 = '添加带有首行缩进的段落';
$section->addText($text1, ['name' => '仿宋', 'size' => 16], ['indentation' => ['firstLine' => 600]]);
$section->addTextBreak(1); // 段落直接换行

设置表格

$table = $section->addTable();

// 定义表格的行和列数
$rowCount = 4; // 行数
$columnCount = 3; // 列数
$table->addRows($rowCount); // 添加行
$table->addColumns($columnCount); // 添加列

// 获取指定的单元格
$cell = $table->getCell(1, 1);
// 设置单元格内容
$cell->addText('Cell 1-1');
// 设置单元格样式
$cell->getStyle()->setBold(true)->setColor('red');
// 设置表格样式
$table->getStyle()->setBorderSize(1)->setBorderColor('000000');

// 合并单元格
$table->mergeCells(1, 1, 2, 1);

// 将表格添加到文档中
$section->add($table);

直接下载

$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
header('pragma:public');
header("Content-Disposition:attachmeng;filename=hello.docx"); // 设置导出保存的文件名
$objWriter->save('php://output');

保存文件

use PhpOffice\PhpWord\IOFactory;

// 创建一个新的 Word 文档对象
$phpWord = new PhpOffice\PhpWord\PhpWord();
// 或者加载现有的 Word 文档
$phpWord=IOFactory::load('path/to/your/test.docx');

// 创建一个 Writer 对象,可以根据需要选择不同的 Writer 类型(如 'Word2007'、'ODText'、'PDF'等)。
$writer = IOFactory::createWriter($phpWord, 'Word2007');

// 保存 Word 文档到指定路径
$savePath = 'path/to/save/your/test.docx';
$writer->save($savePath);