TP架構如何?php資料匯出word詳解

來源:互聯網
上載者:User
本文主要和大家分享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檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.