require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
//定义TOC字体样式
$fontStyle = array('spaceAfter'=>60,'size'=>12);
//添加标题样式
$PHPWord->addTitleStyle(1,array('size'=>20,'color'=>'333333','bold'=>true));
$PHPWord->addTitleStyle(2,array('size'=>16,'color'=>'66666'));
//添加文本
$section->addText('Table of contents:');
$section->addTextBreak(2);
//添加TOC
$section->addToc($fontStyle);
//添加标题
$section->addPageBreak();
$section->addTitle('This is the title');
$section->addText('Some text...');
$section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 1',2);
$section->addTextBreak(2);
$section->addText('Some More text...');
$section->addTextBreak(2);
$section->addTitle('Anoter Title (Title 2)',1);
$section->addText('Some text...');
$section->addTextBreak(2);
$section->addTitle('I am Title 3',1);
$section->addText('And more text...');
$section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 3',2);
$section->addText('Again and Again, More text...');
//保存文件
$objWriter = PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
$objWriter->save('TitleTOC.docx');
?>