require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
//定义样式数组
$styleTable = array(
'borderSize'=>6,
'borderColor'=>'006699',
'cellMargin'=>80
);
$styleFirstRow = array(
'borderBottomSize'=>18,
'borderBottomColor'=>'0000ff',
'bgColor'=>'66bbff'
);
//定义单元格样式数组
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center','textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
//定义第一行的字体
$fontStyle = array('bold'=>true,'align'=>'center');
//添加表格样式
$PHPWord->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow);
//添加表格
$table = $section->addTable('myOwnTableStyle');
//添加行
$table->addRow(900);
//添加单元格
$table->addCell(2000,$styleCell)->addText('Row 1',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row 2',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row 3',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row 4',$fontStyle);
$table->addCell(2000,$styleCellBTLR)->addText('Row 5',$fontStyle);
//添加更多的行/单元格
for($i=1;$i<=10;$i++){
$table->addRow();
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$table->addCell(2000)->addText("Cell $i");
$text = ($i%2==0) ? 'X' : '';
$table->addCell(500)->addText($text);
}
//保存文件
$objWriter = PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
$objWriter->save('Table.docx');
【PHPWord】创建带样式表格的Word文档
最新推荐文章于 2024-07-29 14:42:41 发布