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"); |