当前位置:Gxlcms > PHP教程 > TP框架如何实现php数据导出word详解

TP框架如何实现php数据导出word详解

时间:2021-07-01 10:21:17 帮助过:5人阅读

本文主要和大家分享TP框架如何实现php数据导出word详解,希望能帮助到大家。

1、使用composer安装phpExcel (composer安装方法→点我)

composer require phpoffice/phpexcel

2、引入相关类

  1. use PHPWord_IOFactory;
  2. use PHPWord;
3、具体实现方法
  1. //调用插件
  2. vendor('PHPWord');
  3. vendor('PHPWord.IOFactory');
  4. $phpWord = new \PhpOffice\PhpWord\PhpWord(); //实例化phpWord类
  5. $properties = $phpWord->getDocInfo();
  6. $properties->setCreator('My name'); //创建者
  7. $properties->setCompany('My factory'); //公司
  8. $properties->setTitle('My title'); //biao
  9. $properties->setDescription('My description'); //描述
  10. $properties->setCategory('My category'); //分类
  11. $properties->setLastModifiedBy('My name'); //最后修改者
  12. $properties->setCreated( mktime(0, 0, 0, 3, 12, 2010) ); //创建时间
  13. $properties->setModified( mktime(0, 0, 0, 3, 14, 2010) ); //修改时间
  14. $properties->setSubject('My subject'); //主题
  15. $properties->setKeywords('my, key, word'); //关键字
  16. $sectionStyle = array(
  17. 'orientation' => null, //页面方向默认竖向
  18. 'marginLeft' => 900,
  19. 'marginRight' => 900,
  20. 'marginTop' => 900,
  21. 'marginBottom' => 900);
  22. $section = $phpWord->addSection($sectionStyle); //创建一个有样式的页面

可用部分样式选项:
borderBottomColor。边框底部的颜色。
borderBottomSize。边框底部尺寸(以缇为单位)。
borderLeftColor。边框留下颜色。
borderLeftSize。边框左侧大小(以缇为单位)。
borderRightColor。边框正确的颜色。
borderRightSize。边框正确尺寸(以缇为单位)。
borderTopColor。边框顶部的颜色。
borderTopSize。边框顶部尺寸(以缇为单位)。
breakType。分节符类型(nextPage,nextColumn,continuous,evenPage,oddPage)。
colsNum。列数。
colsSpace。列之间的间距。
footerHeight。页脚底部的间距。
gutter。页面排水沟间距。
headerHeight。间距到标题顶部。
marginTop。页面页边距(以缇为单位)。
marginLeft。页面空白(以缇为单位)。
marginRight。正确的页边距(以缇为单位)。
marginBottom。页边距底部(以缇为单位)。
orientation。页面方向(portrait,这是默认的,或landscape)。
pageSizeH。页面高度(以缇为单位)。由orientation选项隐式定义。不鼓励任何改变。

pageSizeW。页面宽度(以缇为单位)。由orientation选项隐式定义。不鼓励任何改变。

  1. //设置文本样式
  2. $fontStyle = [
  3. 'bgColor' => 'red', //字体背景色
  4. 'bold' => true, //是否加粗
  5. 'size' => '20',
  6. 'color' => 'green', //字体颜色
  7. 'doubleStrikethrough' => true //双实线
  8. ];
  9. //设置段样式
  10. $paragraphStyle = [
  11. 'bidi' => true, //从左到左
  12. ];
  13. $text = '所添加的文本内容';
  14. //添加文本
  15. $section->addText($text, $fontStyle, $paragraphStyle);

可用字体样式选项:
allCaps。全部大写,对或错。
bgColor。字体背景颜色,例如FF0000。
bold。大胆,真实或虚假。
color。字体颜色,例如FF0000。
doubleStrikethrough。双删除线,真或假。
fgColor。字体突出显示颜色,例如黄色,绿色,蓝色。
请参阅\PhpOffice\PhpWord\Style\Font::FGCOLOR_...常量以获取更多值
hint。字体内容类型,默认,eastAsia或cs。
italic。斜体,真或假。
name。字体名称,例如Arial。
rtl。从右到左的语言,真或假。
size。字体大小,例如20,22。
smallCaps。小型大写,真或假。
strikethrough。删除线,真或假。
subScript。下标,真或假。
superScript。上标,对或错。
underline。下划线,单个,短划线,点缀等
请参阅\PhpOffice\PhpWord\Style\Font::UNDERLINE_...常量以获取更多值
lang。语言,如en-US,fr-BE等语言代码或者如果需要设置eastAsian或双向语言的对象(或数组)

查看\PhpOffice\PhpWord\Style\Language一些语言代码的类。

可用的段落样式选项:
alignment。支持自ECMA-376标准第1版以来的所有对齐模式,直到ISO / IEC 29500:2012。
查看\PhpOffice\PhpWord\SimpleType\Jc课程的细节。
basedOn。父母的风格。
hanging。挂多少钱。
indent。缩小多少。
keepLines。将所有行保留在一页上,为true或false。
keepNext。用下一段保留段落,对或错。
lineHeight。文本行的高度,例如1.0,1.5等等
next。下一款的风格。
pageBreakBefore。在下一页开始段落,是true还是false。
spaceBefore。段落之前的空格。
spaceAfter。段落后的空格。
spacing。线条之间的空间。
spacingLineRule。行间距规则。自动,确切,atLeast
tabs。一套自定义选项卡停止。
widowControl。允许第一行/最后一行显示在单独的页面上,true或false。
contextualSpacing。在使用相同样式时,忽略上下方向的间距,true或false。
bidi。从右到左的段落布局,true或false。
shading。段落阴影。
textAlignment。线上的垂直字符对齐。

查看\PhpOffice\PhpWord\SimpleType\TextAlignment课程可能的值。

  1. //添加标题(相关样式需要单独设置)
  2. $phpWord->addTitleStyle(1, $fontStyle, $paragraphStyle);
  3. $section->addTitle('所添加的标题内容', 1);

添加标题:

$phpWord->addTitleStyle($depth, [$fontStyle], [$paragraphStyle]);

$section->addTitle($text, [$depth]);

  1. $linkSrc = 'https://www.baidu.com'; //链接地址
  2. $linkName = '百度搜索'; //链接名称
  3. //添加超链接(相关样式需要单独设置)
  4. $section->addLink($linkSrc, $linkName, $fontStyle, $paragraphStyle);

添加超链接:

$section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]);

  1. //添加页脚方法
  2. $footer = $section->addFooter();
  3. $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.'); //向页眉或页脚添加页码或页数
  4. $breakCount = 10; //设置换行数
  5. $section->addTextBreak($breakCount, $fontStyle, $paragraphStyle); //设置换行
  6. $section->addPageBreak(); //添加换页符
  7. $section->addListItem("list1", 1, $fontStyle, $paragraphStyle); //创建列表
  8. $section->addListItem("list2", 1, $fontStyle, $paragraphStyle);
  9. $section->addListItem("list3", 1, $fontStyle, $paragraphStyle);
  10. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  11. $objWriter->save('AA.docx'); //生成word文件

相关推荐:

迅速掌握PHP导出Word文档原理_PHP教程

php导出word形式

php导出word格式数据的范例代码

以上就是TP框架如何实现php数据导出word详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行