TP框架實現php數據導出word

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文件


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章