编辑
2026-04-01
undefined
00

目录

简介
使用

简介

PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。

项目地址:https://github.com/PHPOffice/PHPWord

使用

// 引入PHPWord类库 require '../vendor/autoload.php'; use \PhpOffice\PhpWord\PhpWord; // 创建Word文档 $phpWord = new PhpWord(); // 设置默认字体和字号 $phpWord->setDefaultFontName('宋体'); $phpWord->setDefaultFontSize(14); //四号字体 // 定义一级标题样式 $phpWord->addTitleStyle(1, ['size' => 16, 'bold' => true]); //三号字体 // 添加段落 $section = $phpWord->addSection(); $section->addText('添加文本', ['size' => 18, 'bold' => true], ['alignment' => 'center', 'spaceAfter' => 240]); $section->addTitle('添加标题', 1); $section->addText('添加文本', null, ['spaceAfter' => 240]); // 添加表格 $table = $section->addTable(['borderSize' => 7.5, 'cellMargin' => 100, 'layout'=> 'fixed']); $table->addRow(); $table->addCell(1200, ['valign' => 'center'])->addText("列1", ['size' => 10.5,'bold' => true],['alignment'=>'center']); $table->addCell(2600,['valign' => 'center'])->addText("列2", ['size' => 10.5,'bold' => true],['alignment'=>'center']); $table->addCell(2600,['valign' => 'center'])->addText("列3", ['size' => 10.5,'bold' => true],['alignment'=>'center']); $table->addCell(2600,['valign' => 'center'])->addText("列4", ['size' => 10.5,'bold' => true],['alignment'=>'center']); $table->addRow(); $table->addCell(null, ['width'=>500])->addText("行A", ['size' => 10.5,'alignment' => 'both']); $cell = $table->addCell(); $cell->addText("行B", ['size' => 10.5,'alignment' => 'both']); $cell->addLink('https://www.baidu.com/', 'https://www.baidu.com/',array('color' => '0000FF','size' => 10.5, 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); $info = '<w:br />行C'; $cell = $table->addCell(); $textrun = $cell->addTextRun(); $textrun->addText('PMID:',['size'=>10.5]); $textrun->addLink('https://www.baidu.com/', '百度', array('color' => '0000FF','size' => 10.5, 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); $table->addCell()->addText("行D", ['size' => 10.5,'alignment' => 'both']); // 保存文档 $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('hello.docx');

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!