phpword使用整理
作者:程序员11 时间:2023-11-03 人气:272 QQ交流群\邮箱:1003265987@qq.com
phpword使用整理
要展示的内容
介绍
PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。PHPWord 的当前版本支持 Microsoft Office Open XML (OOXML 或 OpenXML)、 OASIS Open Document Format for Office Applications (OpenDocument 或 ODF)、 Rich Text Format (RTF)、 HTML 和 PDF。
安装
composer require phpoffice/phpword
安装效果
创建文档
$phpWord = new PhpOfficePhpWordPhpWord(); // 向文档添加任何元素都必须先添加 Section $section = $phpWord->addSection(); // 添加默认字体样式Text 元素 $section->addText( '正文内容正文内容正文内容正文内容正文内容正文内容正文内容正文内容正文内容' );
设置默认字体和字号
$phpWord->setDefaultFontName('宋体'); $phpWord->setDefaultFontSize(10);
设置文本样式
// 设置段落样式:通过自定义内联字体 $section->addText( '段落1', array('name' => '宋体', 'size' => 30, 'bold' => true) );
// 设置段落样式:通过使用命名字体样式自定义字体 $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' => '宋体', 'size' => 20, 'color' => 'red', 'bold' => true) ); $section->addText( '段落3', $fontStyleName );
3.使用显式创建的字体样式对象
// 设置段落标题:使用显式创建的字体样式对象自定义字体的Text元素 $fontStyle = new PhpOfficePhpWordStyleFont(); $fontStyle->setBold(true); $fontStyle->setName('宋体'); $fontStyle->setSize(20); $fontStyle->setColor('red'); $myTextElement = $section->addText('段落标题2'); $myTextElement->setFontStyle($fontStyle);
编号标题
$phpWord->addNumberingStyle( 'hNum', array('type' => 'multilevel', 'levels' => array( array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'), array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'), array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'), ) ) ); $phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0, 'align' => 'center')); $phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1)); $phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2)); $section->addTitle('标题1', 1); $section->addTitle('标题2', 2); $section->addTitle('标题3', 3);
效果:
换行符
$section->addTextBreak(1);
分页符
$section->addPageBreak();
超链接
$textrun = $section->addTextRun('Heading1'); $textrun->addText('The is '); $textrun->addLink('https://blog.csdn.net/json_ligege', 'My Blog', 'Link'); // Link $section->addLink('https://blog.csdn.net/json_ligege', 'CSDN', 'Link', 'Heading2');
效果:
创建表格
$styleTable = array('borderSize' => 6, 'borderColor' => 'red', 'cellMargin' => 80); // 定义表格样式 $phpWord->addTableStyle('table_1', $styleTable);
文字样式
$fontStyle = array('bold' => true, 'align' => 'center');
定义表格
$table = $section->addTable('table_1'); // 定义表格宽度 $width = 1000; $table->addRow(40); $table->addCell($width)->addText("ID", $fontStyle); $table->addCell($width)->addText("名称", $fontStyle); $table->addCell($width)->addText("性别", $fontStyle); $table->addCell($width)->addText("年龄", $fontStyle); $table->addCell($width)->addText("爱好", $fontStyle); $arr = [ ['id' => 1, 'name' => '张三', 'gender' => '男', 'age' => 18, 'hobby' => '足球'], ['id' => 2, 'name' => '张四', 'gender' => '女', 'age' => 18, 'hobby' => '跑步'], ['id' => 3, 'name' => '账务', 'gender' => '男', 'age' => 18, 'hobby' => '羽毛球'], ]; foreach ($arr as $k => $v) { $table->addRow(); $table->addCell($width)->addText($v['id']); $table->addCell($width)->addText($v['name']); $table->addCell($width)->addText($v['gender']); $table->addCell($width)->addText($v['age']); $table->addCell($width)->addText($v['hobby']); }
效果:
添加图片
$section->addImage('./images/word1.jpg', array( 'width' => 100, 'height' => 100, 'marginTop' => 100, 'marginLeft' => 100, 'wrappingStyle' => 'behind' ));
添加水印图
$header = $section->addHeader(); $header->addWatermark('./images/word1.jpg');
添加文件对象
$section->addOLEObject('./test.cls');
效果:
文件保护
$documentProtection = $phpWord->getSettings()->getDocumentProtection(); $documentProtection->setEditing(PhpOfficePhpWordSimpleTypeDocProtect::READ_ONLY); $documentProtection->setPassword('123456');
加载word文件
$file = './uploads/contract/test.docx'; // 加载word文档,使用phpword处理 $phpWord = PhpOfficePhpWordIOFactory::load($file);
内容转化为html
$xmlWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, "HTML"); $path = pathinfo($file); $fileName = './uploads/tmp/' . $path['filename'] . '.html'; $xmlWriter->save($fileName);
保存
$objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('test.docx');
保存为ODF
$objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'ODText'); $objWriter->save('test.odt');
保存为HTML
$objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'HTML'); $objWriter->save('test.html');
// 创建新的文档... $phpWord = new PhpOfficePhpWordPhpWord(); $section = $phpWord->addSection(); $section->addText('文章正文文章正文文章正文文章正文'); $file = 'test.doc'; header("Content-Description: File Transfer"); header('Content-Disposition: attachment; filename="' . $file . '"'); header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); $xmlWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007'); $xmlWriter->save("php://output");
效果:
模板替换
格式
加载模板
// 模板的路径 $path = 'template.docx'; // 声明一个模板对象、读取模板 $templateProcessor = new PhpOfficePhpWordTemplateProcessor($path);
替换字符串

替换并保存
// 模板的路径 $path = 'template.docx'; // 声明一个模板对象、读取模板 $templateProcessor = new PhpOfficePhpWordTemplateProcessor($path); // 替换模板中的变量,对应word里的 ${title} $templateProcessor->setValue('title','简历'); $templateProcessor->setValue('name','张三'); $templateProcessor->setValue('gender','男'); // 生成word路径,根据自己的目录调整 $filePath= './test.docx'; // 生成新的word $templateProcessor->saveAs($filePath);
替换图片
$templateProcessor->setImageValue('img', ['path' => 'cover.jpg']);
$templateProcessor->setImageValue('img', [ 'path' => 'cover.jpg', 'width'=>200, 'height'=>200 ]);
替换表格
$arr = [ ['id' => 1, 'name' => '龙傲天','class' => '一班', 'age' => '19', 'score' => 100], ['id' => 2, 'name' => '张武','class' => '二班', 'age' => '19', 'score' => 100], ['id' => 3, 'name' => '李云','class' => '三班', 'age' => '19', 'score' => 100], ['id' => 4, 'name' => '宋清','class' => '四班', 'age' => '19', 'score' => 100], ];
// 模板的路径 $path = 'template.docx'; // 声明一个模板对象、读取模板 $templateProcessor = new PhpOfficePhpWordTemplateProcessor($path); // 复制行 $templateProcessor->cloneRow('id', count($arr)); // 替换变量 foreach ($arr as $k => $v) { $templateProcessor->setValue('id#' . ($k + 1), $v['id']); $templateProcessor->setValue('name#' . ($k + 1), $v['name']); $templateProcessor->setValue('class#' . ($k + 1), $v['class']); $templateProcessor->setValue('age#' . ($k + 1), $v['age']); $templateProcessor->setValue('score#' . ($k + 1), $v['score']); } // 生成word路径,根据自己的目录调整 $filePath = './test.docx'; // 生成新的word $templateProcessor->saveAs($filePath);
总结
Welcome to PHPWord’s documentation — PHPWord 0.18.2 documentation
链接:https://blog.csdn.net/json_ligege/article/details/130993122
温馨提示:
欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。