使用PHPWord,您可以使用PHP 5.3.3+脚本动态创建OOXML,ODF或RTF文档。以下是您可以使用PHPWord库执行的一些
操作:
设置文档属性,例如标题,主题和创建者。
使用不同的设置创建文档部分,例如纵向/横向,页面大小和页面编号
为每个部分创建页眉和页脚
设置默认字体类型,字体大小和段落样式
使用UTF-8和东亚字体/字符
将自定义字体样式(例如粗体,斜体,颜色)和段落样式(例如居中,多列,间距)定义为命名样式或文本内联
插入段落,可以是包含其他元素的简单文本或复杂文本(文本运行)
插入标题(标题)和目录
插入文本分隔符和分页符
插入和格式化图像,本地,远程或页面水印
插入二进制OLE对象,如Excel或Visio
插入并格式化每个行的自定义属性(例如,重复为标题行)和单元格(例如背景颜色,rowspan,colspan)
将列表项插入项目符号,编号或多级
插入超链接
插入脚注和尾注
插入绘图形状(弧形,曲线,直线,折线,矩形,椭圆形)
插入图表(饼图,圆环图,条形图,线条图,区域图,散点图,雷达图)
插入表单字段(textinput,checkbox和dropdown)
从模板创建文档
使用XSL 1.0样式表来转换OOXML模板的标题,主文档部分和页脚
…以及更多有关进展的功能
要求
PHPWord需要以下内容:
PHP 5.3.3+
XML Parser扩展
Zend \ Escaper组件
Zend \ Stdlib组件
Zip扩展(可选,用于编写OOXML和ODF)
GD扩展(可选,用于添加图像)
XMLWriter扩展(可选,用于编写OOXML和ODF)
XSL扩展(可选,用于将XSL样式表应用于模板)
dompdf库(可选,用于编写PDF)
安装
composer require phpoffice/phpword
composer require phpoffice/phpword:dev-master //安装最新版本
您当然也可以手动编辑composer.json文件
"require": {
"phpoffice/phpword": "v0.16.*"
}
}
入门
以下是PHPWord库的基本用法示例。
<?php
require_once __DIR__.'/../bootstrap/autoload.php';
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* Note: any element you append to a document must reside inside of a Section. */
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
);
/*
* Note: it's possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element with font customized inline...
$section->addText(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10)
);
// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
$fontStyleName
);
// 正在添加使用显式创建的字体样式对象自定义字体的文本元素…
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
// 保存为OOXML 文件
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
// 保存为ODF 文件
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// 保存为html文件
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* Note: 我们跳过RTF,因为它不是基于XML的,需要一个不同的示例. */
/* Note: 我们跳过PDF,因为“html to pdf”方法用于创建PDF文档. */
- 读取word文档修改后保存
<?php
use PhpOffice\PhpWord\TemplateProcessor;
require_once __DIR__.'/../bootstrap/autoload.php';
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
//载入模板文件
$document = new TemplateProcessor('helloWorld.docx');
//替换Word模板文件中的一处内容,这里如果要显示中文,则必须使用iconv函数
// 变量格式可以自己设置例如: ${name}
$document->setValue('${name}', iconv('utf-8', 'utf-8','我是替换呵呵'));
$filename = 'test.docx';
//保存Word文档
$document->saveAs($filename);
//输出下载Word文档
https://mp.csdn.net/mdeditor/99973661
- 读取word文档,修改后发送给客服端