本文主要和大家分享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文件
相關推薦:
#以上是TP框架如何實作php資料導出word詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6
視覺化網頁開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版