ephpoffice / phpword
PHPWord - 使用PHP读取、创建和写入Word文档
0.7.0
2014-01-28 07:03 UTC
Requires
- php: >=5.3.0
- ext-xml: *
This package is auto-updated.
Last update: 2024-09-21 21:01:39 UTC
README
OpenXML - 使用PHP读取、写入和创建Word文档。
PHPWord是一个用PHP编写的库,用于创建Word文档。
因为生成的结果是docx文件(Office Open XML),可以被所有主流办公软件打开,所以无需Windows操作系统即可使用。
想要贡献? 分支我们吧!
要求
- PHP版本5.3.0或更高
安装
建议您通过composer安装PHPWord库。为此,请将以下行添加到您的composer.json
中。
{ "require": { "phpoffice/phpword": "dev-master" } }
文档
目录
基本用法
以下是一个PHPWord库的基本示例。
$PHPWord = new PHPWord(); // Every element you want to append to the word document is placed in a section. // To create a basic section: $section = $PHPWord->createSection(); // After creating a section, you can append elements: $section->addText('Hello world!'); // You can directly style your text by giving the addText function an array: $section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); // If you often need the same style again you can create a user defined style // to the word document and give the addText function the name of the style: $PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); $section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); // You can also put the appended element to local object like this: $fontStyle = new PHPWord_Style_Font(); $fontStyle->setBold(true); $fontStyle->setName('Verdana'); $fontStyle->setSize(22); $myTextElement = $section->addText('Hello World!'); $myTextElement->setFontStyle($fontStyle); // Finally, write the document: $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('helloWorld.docx');
度量单位
Open Office XML的基本长度单位是twip。twip表示“英寸点二十分之一”,即1 twip = 1/1440英寸。
您可以使用PHPWord辅助函数将英寸、厘米或点转换为twip。
// Paragraph with 6 points space after $PHPWord->addParagraphStyle('My Style', array( 'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6)) ); $section = $PHPWord->createSection(); $sectionStyle = $section->getSettings(); // half inch left margin $sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5)); // 2 cm right margin $sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
部分
Word中每个可见元素都放置在部分内部。要创建部分,请使用以下代码
$section = $PHPWord->createSection($sectionSettings);
$sectionSettings
是一个可选的关联数组,用于设置部分。示例
$sectionSettings = array( 'orientation' => 'landscape', 'marginTop' => 600, 'colsNum' => 2, );
部分设置
以下是可以用于部分的可用设置
orientation
页面方向,即'portrait'(默认)或'landscape'marginTop
页面顶部边距,以twip为单位marginLeft
页面左侧边距,以twip为单位marginRight
页面右侧边距,以twip为单位marginBottom
页面底部边距,以twip为单位borderTopSize
顶部边框大小,以twip为单位borderTopColor
顶部边框颜色borderLeftSize
左侧边框大小,以twip为单位borderLeftColor
左侧边框颜色borderRightSize
右侧边框大小,以twip为单位borderRightColor
右侧边框颜色borderBottomSize
底部边框大小,以twip为单位borderBottomColor
底部边框颜色headerHeight
页眉上方间距footerHeight
页脚下方间距colsNum
列数colsSpace
列间距breakType
部分断点类型(nextPage、nextColumn、continuous、evenPage、oddPage)
以下两个设置由orientation
设置自动设置。您可以更改它们,但不太推荐。
pageSizeW
页面宽度,以twip为单位pageSizeH
页面高度,以twip为单位
部分页码
您可以更改部分页码。
$section = $PHPWord->createSection(); $section->getSettings()->setPageNumberingStart(1);
文本
可以通过使用addText
和createTextRun
方法添加文本。 addText
用于创建只包含文本且样式相同的简单段落。createTextRun
用于创建包含不同样式(有些加粗,有些斜体等)或其它元素(如图片或链接)的复杂段落。
addText
示例
$fontStyle = array('name' => 'Times New Roman', 'size' => 9); $paragraphStyle = array('align' => 'both'); $section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
createTextRun
示例
$textrun = $section->createTextRun(); $textrun->addText('I am bold', array('bold' => true)); $textrun->addText('I am italic, array('italic' => true)); $textrun->addText('I am colored, array('color' => 'AACC00'));
属性
size
文本大小,例如20、22、name
字体名称,例如Arialbold
文本加粗,true或falseitalic
文本斜体,true或falsesuperScript
文本是上标,true 或 falsesubScript
文本是下标,true 或 falseunderline
文本是下划线,true 或 falsestrikethrough
文本是删除线,true 或 falsecolor
文本颜色,例如 FF0000fgColor
前景色line-height
文本行高,例如 1.0,1.5 等...
段落样式
属性
line-height
文本行高,例如 1.0,1.5 等...align
段落对齐,left,right 或 centerspaceBefore
段落前的空格spaceAfter
段落后的空格tabs
自定义制表位集合indent
缩进量
表格
以下说明如何创建表格。
$table = $section->addTable(); $table->addRow(); $table->addCell();
单元格样式
单元格跨行
您可以在多列上跨单元格。
$cell = $table->addCell(200); $cell->getStyle()->setGridSpan(5);
图片
您可以使用以下示例轻松添加图片。
$section = $PHPWord->createSection(); $section->addImage('mars.jpg');
属性
width
像素宽度height
像素高度align
图片对齐,left,right 或 centermarginTop
顶部边距,以英寸为单位,可以是负值marginLeft
左侧边距,以英寸为单位,可以是负值wrappingStyle
可以是 inline,square,tight,behind,infront
要添加具有属性的图片,请考虑以下示例。
$section->addImage( 'mars.jpg', array( 'width' => 100, 'height' => 100, 'marginTop' => -1, 'marginLeft' => -1, 'wrappingStyle' => 'behind' ) );