使用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。

php $keyword,phpword导出word相关推荐

  1. PHPword 导出word设置不同样式

    ** PHPword 导出word设置不同样式 ** 1.先看需求样子: 2.先用composer下载phpword到你的项目中,如果不会可以到网上搜,教程很多: 3. 引入,然后设置下全局字体字号, ...

  2. PHPWord导出word文档

    最近接了个把数据导出到word文档的需求,之前一直都是使用PHPExcel库导出excel的,还是头次接到导出到word文档的需求,我想既然有PHPExcel,那么肯定也会有PHPWord库吧,在网上 ...

  3. php导出word文件(二)

    php用phpword导出word文件的方式有两种:1.完全在php控制器里操作,这种方法适合内容较少,样式较为简单的文档.2.先读取word文档,然后在php控制器里替换原有文档的数据,这种方法不会 ...

  4. PHP导出word方法(一phpword)

    背景 上一篇:MHT导出word文档 上一篇,已经记录了使用MHT来生成并导出word文档的方法,这次,来记录一下使用php的扩展phpword来生成并导出word: 使用步骤 gitlab上comp ...

  5. php导出word html转换为word文档 (原生+phpword)

    将html内容转换成word文档,有两种实现方式 1.直接输入html内容到word文档中 优点:简单粗暴,基本上支持所有html标签和样式 缺点:无法操作word文档其他功能,例如页码,页眉,目录之 ...

  6. java导出word文档_PHPWord导出word文档

    既然有PHPExcel,那么肯定也会有PHPWord库,且都是phpoffice家的.看了下文档,最终决定使用模板的方式来导出数据,感觉也是最简单的一种方式了. 过程如下: 使用composer下载P ...

  7. php打开word文档_PHP使用phpword生成word文档

    写在前边的话 使用phpword生成文档有两种方式 直接使用代码编写word文档,用代码生成word,但是设置样式,格式,图片非常麻烦,不建议使用.如果客户或产品提供一份word的样式,我们也难以完全 ...

  8. PHP 生成 ppt,php生成导出Word、Excel、PowerPoint插件

    php生成导出Word.Excel.PowerPoint插件 国外的一个插件,赞! 官网及下载地址: 实例:<?php require_once '../PHPWord.php'; // New ...

  9. PHP导出word方法(一mht)

    相关文章: phpword生成word文档,phpword的使用 背景 项目中有需要自定义样式,导出用户的简历的功能,简历嘛,最好是导出word或者pdf的,最后选择了导出成word 调研 导出wor ...

最新文章

  1. 异常记录与处理-Cannot find class [org.apache.commons.dbcp.BasicDataSource]
  2. 如何使用robots.txt[摘自网络]
  3. 【操作系统】分页内存管理
  4. poj1979 深度优先搜索 挑战程式设计竞赛
  5. 手把手教你用Python的NumPy包处理数据
  6. axios请求拦截器错误_React中使用高阶组件和axios的拦截器,统一处理请求失败提示...
  7. 重定向与请求转发的区别
  8. Windows程序设计(5):移动窗口、调整窗口大小
  9. 产品经理面试必备常见问题及解析
  10. python36 +tkinter制作12306火车票查询系统
  11. JAVA流的使用(复制文件效率对比)初学者
  12. vbox vdi磁盘注册_克隆Virtualbox的vdi硬盘 和 修改uuid
  13. 云计算工程师面试题集锦,常见云计算面试题及答案
  14. 微信小程序超级占内存_实测:微信小程序究竟会占用多少内存?结果竟然
  15. Java 的反射机制
  16. Java 注解 (Annotation)
  17. 海思处理器sensor驱动调用过程与源码解析与实战
  18. 2019年考研数学一真题pdf ​​​
  19. 广东省科技进步奖二等奖!天翼物联NB-IoT技术助推万物智联
  20. FlexSlider js——轮播

热门文章

  1. 3D-2D PnP相关算法
  2. HTML5表格介绍以及基本应用
  3. 新闻稿标题怎么写才能引发共鸣?
  4. java最长回文子序列_在一个字符串里面怎么找出最长回文子序列长度
  5. 陀螺解读 | 深圳区块链政策:千呼万唤始出来,好饭不怕晚
  6. 一、对象的属性局部变量和全局变量0711
  7. CentOS 7 查看关闭防火墙
  8. (建议精读)灵魂之力—提升学习效率 99% 的私藏秘籍全公开
  9. 【DIY】远程手机遥控车 androidbot
  10. 初识Adobe After Effects效果制作