php $keyword,phpword导出word

这篇博客介绍了如何使用PHPWord库通过Composer安装并创建Word文档。内容包括设置默认字体和字号、配置文档信息、添加段落、文本、分隔符、列表、表格、图片、对象、标题、目录以及保存文档。示例代码详细展示了各种操作方法。
摘要由CSDN通过智能技术生成

使用composer安装PHPword,composer require phpoffice/phpword

phpword的安装目录在vender中,在使用的时候直接new就可以了

1.开始新建phpword$php_word = new Phpword();

如果你直接复制这段代码一定会报错,因为这里没有写引用的代码,需要在class外层添加引用的代码也就是我们说的use,但是从此处开始我将直接将需要引用的地方写到new中。$php_word = new \PhpOffice\PhpWord\PhpWord();

在new之后可以进行基本的设置,

2.配置//配置字体

$php_word->setDefaultFontName('微软雅黑');

//配置全局的字号

$php_word->setDefaultFontSize(12);

还可以配置一些word的基本信息$properties = $php_word->getDocInfo();

$properties->setCreator('My name');

$properties->setCompany('My factory');

$properties->setTitle('My title');

$properties->setDescription('My description');

$properties->setCategory('My category');

$properties->setLastModifiedBy('My name');

$properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));

$properties->setModified(mktime(0, 0, 0, 3, 14, 2014));

$properties->setSubject('My subject');

$properties->setKeywords('my, key, word');

3.添加section$section = $php_word->addSection();

在添加section的时候同时可以添加一些style进去

+ borderBottomColor. 边框颜色.

+ borderBottomSize. 边框宽度 (in twips).

+ borderLeftColor. 左侧边框颜色.

+ borderLeftSize. 左侧边框宽度(in twips).

+ borderRightColor. 右侧边框颜色.

+ borderRightSize. 右侧边框宽度(in twips).

+ borderTopColor. 上边框颜色.

+ borderTopSize. 上边框宽度(in twips).

+ breakType. 换行格式 (nextPage, nextColumn, continuous, evenPage, oddPage).

+ colsNum. 列数目.

+ colsSpace. 列间距.

+ footerHeight. 底部间距.

+ gutter. 每页间距.

+ headerHeight. 头间距.

+ marginTop. 上边距(in twips).

+ marginLeft. 左边距(in twips).

+ marginRight. 右边距(in twips).

+ marginBottom. 下边距(in twips).

+ orientation. 页面方向:默认竖向:null 横向:landscape.

+ pageSizeH. 页面高度(in twips).

+ pageSizeW. 页面宽度(in twips).

4.添加文本$section->addText('我是一段话', [

'color' => '#000',

'size' => 40,

'bold' => true

], [

'align' => 'center',

'spacing' => 10

]);

这个就是添加文本的方式$section->addText('text',[font_style],[paragraph_style]);

本文的所有fontstyle,paragraphstyle都和此处的注解一致

font_styleallCaps. 首字母大写, true or false.

bgColor. 文字背景色, e.g. FF0000.

bold. 加粗, true or false.

color. 文字的颜色, e.g. FF0000.

doubleStrikethrough. 双删除线, true or false.

fgColor. 文字突出颜色, e.g. yellow, green, blue.

hint. 文字内容类型(我暂时没用到过), default, eastAsia, or cs.

italic. 斜体, true or false.

name. 字体name, e.g. Arial.

rtl. 从右到左的文本, true or false.

size. 字号, e.g. 20, 22和word中的字号值一致.

smallCaps. 小写, true or false.

strikethrough. 删除线, true or false.

subScript. 下标, true or false.

superScript. 上标, true or false.

underline. 下划线, dash, dotted, etc.

paragraph_stylealign. 对齐方式, center or left or right.

spacing. 间距,(in twips).

5.添加分隔符//换行符

$section->addTextBreak(2, [

'color' => '#000',

'size' => 40,

'bold' => true

], [

'align' => 'center',

'spacing' => 10

]);

//分页符

$section->addPageBreak();

//官方实例

$section->addTextBreak([breakCount], [font_style], [paragraph_style]);

这里的breakCount就是一个int,font_style及paragraph_style和text中一致,分页符没有过多的说明。

6.添加列表$list_style = [

'type' => 'multilevel',

'levels' => [

['format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360],

['format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720],

]

];

$section->addListItem('列表1', 0, null, $list_style);

$section->addListItem('列表a', 1, null, $list_style);

$section->addListItem('列表b', 1, null, $list_style);

$section->addListItem('列表2', 0, null, $list_style);

ex:$section->addListItem(text, [depth], [font_style], [list_style], [paragraph_style]);

list的style我没有找到更多的注解,就先拿例子中的注解一下吧

list_styletype. 类型 hybridMultilevel or multilevel.

levels. 级别 (arr).

format. 格式(decimal or lowerLetter or lowerRoman or upperLetter or bullet).

text. 文本(%+int).

left. 左边距(in twips).

hanging. 悬挂(in twips).

tabPos. 可以理解为缩进(in twips).

7.添加表格$table = $section->addTable([

'width' => '9639',

'borderSize' => 6,

'cellMargin' => 50

]);

$table->addRow();

$table->addCell('1134')->addText('hhhh', $font_style_12, $paragraph_style);

$table->addCell('8505', $cell_col_span)->addText('wwww', $font_style_12, $paragraph_style);

这个是表格添加的基本方式,这里只对table_style进行注解

单元格中也有这样的一些样式控制

table_stylebgColor. 背景色, e.g. ‘9966CC’.

border(Top|Right|Bottom|Left)Color. 边框颜色, e.g. ‘9966CC’.

border(Top|Right|Bottom|Left)Size. 边框宽度 in twips.

gridSpan. 跨行.

textDirection(btLr|tbRl). 文本方向. You can use constants \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR and \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL

valign. 垂直对齐, top, center, both, bottom.

vMerge. 垂直合并,restart or continue.

width. 单元格宽 in twips.

cantSplit. 表格行不能在页面之间折断, true or false.

exactHeight. 行高设定.

tblHeader. 在每个新页面上重复表格行, true or false.

8.Images$section->addImage('10001_1.jpg', $image_style);

image_styleheight. 高度 in pixels.

marginLeft. 左边距 in inches, can be negative.

marginTop. 上边距 in inches, can be negative.

width. 宽度 in pixels.

wrappingStyle. 图片格式, inline, square, tight, behind, or infront.

9.对象$section->addObject( $src, [$style] );

stylealign. 对齐方式, left – right – center

10.标题$section->addTitle( $text, [$depth] );

11.目录$styleTOC = ['tabLeader'=>PHPWord_Style_TOC::TABLEADER_DOT];

$styleFont = ['spaceAfter'=>60, 'name'=>'Tahoma', 'size'=>12];

$section->addTOC($styleFont, $styleTOC);

12.生成word$php_word->save($file_name,'Word2007',false);

生成word,

下载的时候需要将false改为true。

这个就是PHPword的基本样式。在写这个文章的时候参考了phpword的文档和djspy。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值