您的位置:首页 > 编程语言 > PHP开发

TP框架实现php数据导出word

2018-03-29 16:31 561 查看
1、使用composer安装phpExcel (composer安装方法→点我composer require phpoffice/phpexcel

2、引入相关类

use PHPWord_IOFactory;
use PHPWord;
3、具体实现方法
//调用插件
vendor('PHPWord');
vendor('PHPWord.IOFactory');
$phpWord  =  new \PhpOffice\PhpWord\PhpWord();      //实例化phpWord类
$properties = $phpWord->getDocInfo();
$properties->setCreator('My name');     //创建者
$properties->setCompany('My factory');    //公司
$properties->setTitle('My title');    //biao
$properties->setDescription('My description');    //描述
$properties->setCategory('My category');    //分类
$properties->setLastModifiedBy('My name');    //最后修改者
$properties->setCreated( mktime(0, 0, 0, 3, 12, 2010) );    //创建时间
$properties->setModified( mktime(0, 0, 0, 3, 14, 2010) );    //修改时间
$properties->setSubject('My subject');     //主题
$properties->setKeywords('my, key, word');    //关键字
$sectionStyle = array(
'orientation' => null,    //页面方向默认竖向
'marginLeft' => 900,
'marginRight' => 900,
'marginTop' => 900,
'marginBottom' => 900);
$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选项隐式定义。不鼓励任何改变。//设置文本样式
$fontStyle = [
'bgColor' => 'red', //字体背景色
'bold' => true, //是否加粗
'size' => '20',
'color' => 'green', //字体颜色
'doubleStrikethrough' => true //双实线
];
//设置段样式
$paragraphStyle = [
'bidi' => true, //从左到左
];
$text = '所添加的文本内容';
//添加文本
$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课程可能的值。//添加标题(相关样式需要单独设置)
$phpWord->addTitleStyle(1, $fontStyle, $paragraphStyle);
$section->addTitle('所添加的标题内容', 1);添加标题:
$phpWord->addTitleStyle($depth, [$fontStyle], [$paragraphStyle]);
$section->addTitle($text, [$depth]);

$linkSrc = 'https://www.baidu.com';     //链接地址
$linkName = '百度搜索';     //链接名称
//添加超链接(相关样式需要单独设置)
$section->addLink($linkSrc, $linkName, $fontStyle, $paragraphStyle);
添加超链接:
$section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]);
//添加页脚方法
$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.'); //向页眉或页脚添加页码或页数
$breakCount = 10;       //设置换行数
$section->addTextBreak($breakCount, $fontStyle, $paragraphStyle);       //设置换行
$section->addPageBreak();   //添加换页符
$section->addListItem("list1", 1, $fontStyle,  $paragraphStyle);    //创建列表
$section->addListItem("list2", 1, $fontStyle,  $paragraphStyle);
$section->addListItem("list3", 1, $fontStyle,  $paragraphStyle);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('AA.docx');    //生成word文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: