欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用PHPWord生成word文檔的方法詳解

 更新時(shí)間:2019年06月06日 10:43:40   作者:月光光  
這篇文章主要介紹了使用PHPWord生成word文檔的方法,結(jié)合實(shí)例形式詳細(xì)分析了PHPWord生成word文檔的具體操作步驟與相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了使用PHPWord生成word文檔的方法。分享給大家供大家參考,具體如下:

有時(shí)我們需要把網(wǎng)頁內(nèi)容保存為Word文檔格式,以供其他人員查看和編輯。PHPWord是一個(gè)用純PHP編寫的庫,使用PHPWord可以輕松處理word文檔內(nèi)容,生成你想要的word文檔。

下載源碼

安裝

我們使用Composer 來安裝PHPWord。

composer require phpoffice/phpword

如何使用

自動(dòng)加載

安裝好phpword后,新建一個(gè)php文檔,引入autoload.php。

require 'vendor/autoload.php';

實(shí)例化

實(shí)例化并新增一個(gè)空白頁。

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();

添加文字內(nèi)容

向空白頁添加文字內(nèi)容,可以設(shè)置文字的樣式,包括字體、顏色、字號(hào)、粗體等等。

$fontStyle = [
  'name' => 'Microsoft Yahei UI',
  'size' => 20,
  'color' => '#ff6600',
  'bold' => true
];
$textrun = $section->addTextRun();
$textrun->addText('你好,這是生成的Word文檔。 ', $fontStyle);

鏈接

可以為Word文檔中的文字添加用于點(diǎn)擊跳轉(zhuǎn)的鏈接。

$section->addLink('https://www.helloweba.net', '歡迎訪問Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
$section->addTextBreak();

圖片

可以在word中添加圖片,如圖片地址logo.png,尺寸為64x64。圖片源也可以是遠(yuǎn)程圖片。

$section->addImage('logo.png', array('width'=>64, 'height'=>64));

頁眉

為Word文檔添加頁眉。

$header = $section->addHeader();
$header->addText('Subsequent pages in Section 1 will Have this!');

頁腳

為word文檔添加頁腳,頁腳內(nèi)容是頁碼,格式居中。

$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));

增加一頁

繼續(xù)增加一頁,加入內(nèi)容。

$section = $phpWord->addSection();
$section->addText('新的一頁.');

表格

增加一個(gè)基礎(chǔ)表格,可以設(shè)置表格的樣式。

$header = array('size' => 16, 'bold' => true);
$rows = 10;
$cols = 5;
$section->addText('Basic table', $header);
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
  $table->addRow();
  for ($c = 1; $c <= 5; $c++) {
    $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
  }
}

生成Word文檔

如果你想生成word文檔放在服務(wù)器上,可以使用:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('hellwoeba.docx');

下載Word文檔

如果你想直接下載Word文檔,不在服務(wù)器上保存的話,可以使用:

$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");

上述代碼會(huì)強(qiáng)制瀏覽器下載為word文檔。

更多有關(guān)PHPWord的內(nèi)容,請參考PHPWord文檔:http://phpword.readthedocs.org/.

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論