一、安装

composer require phpoffice/phpword

二、基本使用

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
/* 使用 */
$phpWord = new PhpWord();
/* 设置全局默认字体 */
$phpWord->setDefaultFontName('仿宋');
$phpWord->setDefaultFontSize(16);
/* 实例化创建新的内容 */
$section = $phpWord->addSection();
/* 自定义字体样式 */
$titleF = array(
    'name'  => '方正小标宋简体',
    'color' => '000000',
    'size'  => 22,
);
/* 段落样式 */
$titleP = array(
    'alignment' => 'center' //start左对齐,end右对齐
);
/* 添加一段内容 */
$section->addText('把第一段当成文档的标题', $titleF, $titleP);
/* 其他段落样式 */
$contentF = array(
    'name' => '仿宋_GB2312', 'size' => 16, 'color' => '000000'
);
$contentP = array(
    'alignment'  => 'left',
    //首行缩进两个16磅大小字体的字符
    'indentation' => array( 'firstLine'=> 2*16*20 )
);
 /* 添加文档内容,使用单独格式;*/
 $section->addText(
     '这是内容的第一个段落,可以很长很很很长很长,反正就是一个段落可以很长很长。。反正就是一个段落随便打点字',
     $contentF,
     $contentP
 );
 $section->addText(
     '第二个段落,也是一样使用一样的格式,一次类推说明一下PHPWord是一个用纯PHP编写的库,它提供了一组用于写入和读取不同文档文件格式的类',
     $contentF,
     $contentP
 );
 /* 写入保存为word 此处为windows本地测试环境,所以直接写死路径,仅作保存查看*/
 $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
 $objWriter->save('C:\Users\helloWorld.docx');

实例:

$phpWord = new PhpWord();
// 全局设置
// 设置字体
$phpWord->setDefaultFontName('宋体');
// 设置字号
$phpWord->setDefaultFontSize(12);
// 文本水平居中
$center = ['alignment' => Jc::CENTER];
// 垂直居中
$cellCenter = ['valign' => 'center'];
$noteStyle = ['name' => '仿宋_GB2312', 'size' => 16];
$noteListStyle = ['name' => '仿宋_GB2312', 'size' => 16, 'lineHeight' => 1.5];
// 添加页面
$section = $phpWord->addSection(['marginLeft' => 750, 'marginRight' => 750]);
// 添加空行
$section->addTextBreak(2);
$section->addText('参与社会公益服务工作记录表', ['name' => '黑体 Regular', 'size' => 22], $center);
$section->addTextBreak(2);
$section->addText('姓名:' . $taskInfo['service']['username'] . '   身份证号码:' . $taskInfo['service']['idcard'] .
    '   联系电话:' . $taskInfo['service']['mobile'] . '', null, ['indent' => 0.7]);
$section->addTextBreak();
$table = $section->addTable(['cellMarginleft' => -250]);
$table->addRow(500);
$table->addCell(2000, $cellCenter)->addText("服务类型", null, $center);
$table->addCell(1400, $cellCenter)->addText("日期", null, $center);
$table->addCell(3000, $cellCenter)->addText("地点", null, $center);
$table->addCell(1200, $cellCenter)->addText("时长", null, $center);
$table->addCell(2000, $cellCenter)->addText("见证人", null, $center);
// 合并行
$cellRowMerge = ['vMerge' => 'restart', 'valign' => 'center'];
$cellRowContinue = ['vMerge' => 'continue'];
// 合并列
$cellColMerge = ['gridSpan' => 5];
$table->addRow(600);
$mergeCell = $table->addCell(2000, $cellRowMerge);
$mergeCell->addText('道路交通', null, $center);
$mergeCell->addText('安全劝导', null, $center);
$taskLogList = $taskInfo['taskLogList'];
$taskLogCount=count($taskLogList);
if(!empty($taskLogList) && $taskLogCount==$taskInfo['service_day']){
    foreach ($taskLogList as $key => $item) {
        if(!empty($item['list'][1]['sign_time'])){
            $hours = (($item['list'][1]['sign_time'] - $item['list'][0]['sign_time']) / 3600);
            if($hours>=2){
                $hours= '2小时';
            }else{
                $hours= '打卡时间不足,无法计算';
            }
        }else{
            $hours = '缺勤,无法计算';
        }
        if ($key == 0) {
            // 日期
            $table->addCell(1400, $cellRowMerge)->addText($item['label'], null, $center);
            // 地点
            $table->addCell(3000, $cellRowMerge)->addText($item['list'][0]['place_text'], null, $center);
            // 时长
            $table->addCell(1200, $cellRowMerge)->addText($hours, null, $center);
            // 见证人
            $table->addCell(2000, $cellRowMerge)->addText($taskInfo['traffic_text'], null, $center);
        } else {
            $table->addRow(600);
            $table->addCell(null, $cellRowContinue);
            $table->addCell(1400, $cellCenter)->addText($item['label'], null, $center);
            $table->addCell(3000, $cellCenter)->addText($item['list'][0]['place_text'], null, $center);
            $table->addCell(1200, $cellCenter)->addText($hours, null, $center);
            $table->addCell(2000, $cellCenter)->addText($taskInfo['traffic_text'], null, $center);
        }
    }
}else{
    for ($i=0;$i<($taskInfo['service_day']-$taskLogCount);$i++) {
        if ($i == 0) {
            // 日期
            $table->addCell(1400, $cellRowMerge)->addText('缺勤', null, $center);
            // 地点
            $table->addCell(3000, $cellRowMerge)->addText('缺勤', null, $center);
            // 时长
            $table->addCell(1200, $cellRowMerge)->addText('缺勤', null, $center);
            // 见证人
            $table->addCell(2000, $cellRowMerge)->addText($taskInfo['traffic_text'], null, $center);
        } else {
            $table->addRow(600);
            $table->addCell(null, $cellRowContinue);
            $table->addCell(1400, $cellCenter)->addText('缺勤', null, $center);
            $table->addCell(3000, $cellCenter)->addText('缺勤', null, $center);
            $table->addCell(1200, $cellCenter)->addText('缺勤', null, $center);
            $table->addCell(2000, $cellCenter)->addText($taskInfo['traffic_text'], null, $center);
        }
    }
}

$table->addRow(600);
$table->addCell(2000, $cellCenter)->addText('接受安全教育', null, $center);
$table->addCell(1400, $cellCenter)->addText('', null, $center);
$table->addCell(3000, $cellCenter)->addText('', null, $center);
$table->addCell(1200, $cellCenter)->addText('', null, $center);
$table->addCell(2000, $cellCenter)->addText('', null, $center);
// 评价
$table->addRow(3000);
$evaluation = $table->addCell(2000, $cellColMerge);
$evaluation->addTextBreak();
$evaluation->addText("评价:", ['cellMargin' => 80], ['indent' => 0.2]);
$evaluation->addTextBreak();
$evaluation->addText("好          良好            合格                不合格", null, ['indent' => 1]);
$evaluation->addTextBreak(3);
$evaluation->addText("年    月    日", null, ['indent' => 6.5]);
$evaluation->addText("(印章)", null, ['indent' => 6.8]);
// 备注
$section->addTextBreak(1);
$section->addText('备注:', $noteStyle);
$section->addText('1、自    年  月  日起3个工作日内到交警部门报到。', $noteListStyle, ['indent' => 0.7]);
$section->addText('2、公益服务时长不少于5/7/9日,每天不少于2个小时,保证在15个工作日内完成。自    年  月  日  至    年  月  日。', $noteListStyle, ['indent' => 0.7]);
$section->addText('3、参与社会公益活动后,积极撰写参与公益活动的心得体会或悔过书,并提交检察机关。', $noteListStyle, ['indent' => 0.7]);
$section->addText('4、报到地点及联系方式:', $noteListStyle, ['indent' => 0.7]);
$section->addText('交警一大队:临沂市兰山区', $noteListStyle, ['indent' => 0.7]);
$section->addText('中队办公室', $noteListStyle, ['indent' => 3.4]);
$section->addText('董警官 0539--0000000', $noteListStyle, ['indent' => 3.4]);
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$report_url = './report/task_' . $task_id . $taskInfo['service_id'] . $taskInfo['traffic_id'] . $taskInfo['place_id'] . $taskInfo['user_id'] . '.docx';

参考网址:

https://www.php.cn/blog/detail/30307.html

https://blog.csdn.net/janthinasnail/article/details/111026003

https://github.com/PHPOffice/PHPWord