使用phpword生成word文檔的兩種方式
使用phpword生成文檔有兩種方式
- 直接使用代碼編寫word文檔,用代碼生成word,但是設(shè)置樣式,格式,圖片非常麻煩,不建議使用。如果客戶或產(chǎn)品提供一份word的樣式,我們也難以完全復(fù)原,調(diào)樣式很頭疼的。
- 讀取原有word模板,替換相關(guān)變量。個人感覺這種方式能滿足絕大部分需求,實(shí)現(xiàn)起來也比較簡單,所有的樣式,格式直接在word模板里設(shè)置好,替換變量就可以了,還可以很方便的切換模板。本文主要介紹這種方式,畢竟我們是為了快速實(shí)現(xiàn)客戶的需求,讓客戶提供一份word模板,我們稍微一改就可以了。
開始干活
1,通過composer安裝phpword包
composer require phpoffice/phpword
2,準(zhǔn)備一個word模板(讓客戶或產(chǎn)品提供吧,docx格式的)
$tpl = 'template/word/display_agreement.docx'; $doc = new TemplateProcessor($tpl);//打開模板 // 簡單替換 $doc->setValue('dealer_name', $oneCust->dealer->dealer_name, 2);//替換變量 第二個參數(shù)替換次數(shù) $doc->setValue('cust_name', $oneCust->customer->cust_name);//替換變量cust_name $doc->setValue('start_time', $arrOneCust['start_time_text']); $doc->setValue('end_time', $arrOneCust['end_time_text']); $doc->setValue('show_day', $arrOneCust['show_day']); $doc->setValue('signing_date', date('Y年m月d日', $arrOneCust['create_at'])); // 陳列要求 // 循環(huán)替換 $arr = [ ['goods_name'=>'蘋果手機(jī)8','specs'=>'128G','number'=>'2臺'], ['goods_name'=>'蘋果手機(jī)11','specs'=>'128G','number'=>'2臺'], ['goods_name'=>'蘋果手機(jī)12','specs'=>'128G','number'=>'2臺'], ] if (!empty($arr)) { $j = 1; $rows = count($arr); $doc->cloneRow('customergoods_name', $rows);//復(fù)制行 foreach ($arr as $oneGoods) { $dTmp = $oneGoods->toArray(); $doc->setValue("customergoods_name#" . $j, "產(chǎn)品名稱:{$oneGoods['goods_name']}");//替換變量 $doc->setValue("customergoods_spce#" . $j, "產(chǎn)品規(guī)格:{$oneGoods['specs']}");//替換變量 $doc->setValue("customergoods_num#" . $j, "數(shù)量:{$oneGoods['number']}");//替換變量 $j++; } }
有時我們需要有“陳列獎勵”數(shù)據(jù)時就顯示沒有時就不顯示,此里需要用到塊標(biāo)簽了與html類似
// 陳列獎勵 // 循環(huán)替換 $arr = [ ['goods_name'=>'蘋果手機(jī)8','time'=>'1606011063','number'=>'2臺'], ['goods_name'=>'蘋果手機(jī)11','time'=>'1606011063','number'=>'2臺'], ['goods_name'=>'蘋果手機(jī)12','time'=>'1606011063','number'=>'2臺'], ] $doc->cloneBlock('WIN_BLOCK',0); if (!empty($arr)) { //顯示塊 $doc->cloneBlock('WIN_BLOCK',1); $j = 1; $rows = count($arr); $doc->cloneRow('customergoods_name', $rows);//復(fù)制行 foreach ($onePhase->customerGoodList as $oneGoods) { $doc->setValue("phase_date#" . $j, date('Y-m-d', $onePhase['time']));//替換變量 $doc->setValue("phase_type#" . $j, '兌付');//替換變量 $doc->setValue("phase_goods#" . $j, $oneGoods['goods_name']);//替換變量 $doc->setValue("phase_num#" . $j, "數(shù)量:{$oneGoods['number']}");//替換變量 $j++; } }
替換圖片
// 只渲染 $tmp->setImageValue('header',['path'=>'1.jpeg']); // 設(shè)置圖片寬高 $tmp->setImageValue('header', ['path' => '1.jpg','width'=>500,'height'=>500]); // 設(shè)置多次替換 $tmp->setImageValue('header', ['path' => '1.jpg','width'=>500,'height'=>500],3);
一些常用的word符號
換行符 <w:br/>
分頁符 <w:br w:type="page"/>
制表符 <w:tab/>
html預(yù)留字符要替換為實(shí)體字符,如&要替換為&,可以使用htmlspecialchars()
使用方式
比如我們數(shù)據(jù)庫存的換行符一般是 \n\r 這個在word中是無效的,要替換為 <w:br/> 才行
$content = str_replace("\r\n", '<w:br />', $content); $doc->setValue('content', $content); //內(nèi)容
到此這篇關(guān)于使用phpword生成word文檔的兩種方式的文章就介紹到這了,更多相關(guān)phpword生成word文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php+ajax無刷新上傳圖片的實(shí)現(xiàn)方法
這篇文章主要介紹了php+ajax無刷新上傳圖片的實(shí)現(xiàn)方法,涉及php結(jié)合ajax進(jìn)行文件傳輸操作相關(guān)技巧,需要的朋友可以參考下2016-12-12PHP實(shí)現(xiàn)二維數(shù)組按某列進(jìn)行排序的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)二維數(shù)組按某列進(jìn)行排序的方法,結(jié)合實(shí)例形式分析了php二維數(shù)組排序的技巧,涉及array_multisort函數(shù)的使用方法,需要的朋友可以參考下2016-11-11WordPress中給媒體文件添加分類和標(biāo)簽的PHP功能實(shí)現(xiàn)
這篇文章主要介紹了WordPress中給媒體文件添加分類和標(biāo)簽的PHP功能實(shí)現(xiàn),同時文中也提到了Media Library Categories這個插件同樣可以達(dá)到目的,需要的朋友可以參考下2015-12-12php5.2以下版本無json_decode函數(shù)的解決方法
這篇文章主要介紹了php5.2以下版本無json_decode函數(shù)的解決方法,需要的朋友可以參考下2014-05-05