APP正在开发中...
收藏
本篇文章介绍使用PHPWord生成word文档的方法。 有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑。PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档。 下载源码 安装 我们使用 Composer 来安装P
require 'vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection();
$fontStyle = [ 'name' => 'Microsoft Yahei UI', 'size' => 20, 'color' => '#ff6600', 'bold' => true ]; $textrun = $section->addTextRun(); $textrun->addText('你好,这是生成的Word文档。 ', $fontStyle);
$section->addLink('https://www.helloweba.net', '欢迎访问Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); $section->addTextBreak();
图片 可以在word中添加图片,如图片地址logo.png,尺寸为64x64。图片源也可以是远程图片。
$section->addImage('logo.png', array('width'=>64, 'height'=>64));
$header = $section->addHeader(); $header->addText('Subsequent pages in Section 1 will Have this!');
$footer = $section->addFooter(); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section = $phpWord->addSection(); $section->addText('新的一页.');
$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}"); } }
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('hellwoeba.docx');
$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");
2019-06-28
2019-10-03
2019-07-04
2019-06-22
2019-02-12