addSection(); //セクションを追加
$fileName = "sample.docx"; //ファイル名を設定
/* テキスト */
$section->addText('標準テキスト');
$section->addText('テキストを太文字にする', array('bold' => true)); //太文字
$section->addText('テキストの色を変更する', array('color' => 'ff0000'));//色
$section->addText('テキストを斜体にする', array('italic' => true)); //斜体
$section->addText('テキストに下線を指定する', array('underline' => 'single')); //下線 single:標準, dash:破線, dotted:ドット
$section->addText('フォントの書体をMS ゴシックに変更する', array('name' => 'MS ゴシック')); //フォント書体
$section->addText('文字の大きさを14ptに変更する', array('size' => 14)); //文字サイズ
$section->addText('フォントの書体、文字サイズ、色、太さ、斜体、下線を一括指定する', array('name' => 'MS ゴシック', 'size' => 12, 'color' => '0000ff', 'bold' => true, 'italic' => true, 'underline' => 'single')); //フォント書体、文字サイズ、色、太文字、斜体、下線、一括指定
$section->addText('左寄せ', array('name' => 'MS 明朝'), array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::START)); //START:左寄せ
$section->addText('中央寄せ', array('name' => 'MS 明朝'), array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); //CENTER:中央寄せ
$section->addText('右寄せ', array('name' => 'MS 明朝'), array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END)); //END:右寄せ
$section->addText('段落を指定する'); //段落
$section->addText('インデントを指定する'); //インデント
$section->addText('テキストを改行する'); //改行
/* テーブル */
$tableStyle = array(
'borderColor' => 'cccccc',
'borderSize' => 5,
'cellMargin' => 30
);
$table = $section->addTable($tableStyle);
$table->addRow(300); //行の高さを指定
$table->addCell(3000, array('bgColor' => 'FFFF66'))->addText('あいうえお');
$table->addCell(3000, array('bgColor' => 'FF6666'))->addText('かきくけこ');
$table->addRow(600);
$table->addCell(3000, array('bgColor' => '66FF66'))->addText('さしすせそ');
$table->addCell(3000, array('bgColor' => '6666FF'))->addText('改行改行');
$table->addRow(600);
$table->addCell(6000, array('bgColor' => 'cccccc'))->addText('スパン');
/* wordファイルのダウンロード */
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //Wordファイル( .docx OFFICE 2007以降)
header('Content-Disposition: attachment; filename="'.$fileName.'"'); //ダウンロード時のファイル名をセット
header('Cache-Control: no-store, max-age=0'); //新しいリソースのキャッシュを防止、キャッシュのクリア
ob_end_clean(); //バッファ消去
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('php://output');
?>