【PHPWord】创建带样式表格的Word文档

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值