PHP生成word文档,表格实例
<?php require_once 'PHPWord.php'; // New Word Document $PHPWord = new PHPWord(); // New portrait section $section = $PHPWord->createSection(); $PHPWord->addFontStyle('rStyle', array('bold'=>true,'color'=>'000000','size'=>16)); $PHPWord->addParagraphStyle('pStyle', array('align'=>'center')); $section->addText('×××公司招聘信息', 'rStyle', 'pStyle'); $section->addTextBreak(2); // Define table style arrays $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80); // Add table style $PHPWord->addTableStyle('myOwnTableStyle', $styleTable); // Add table $table = $section->addTable('myOwnTableStyle'); $fontStyle = array('bold'=>true, 'align'=>'center'); // Add more rows / cells $table->addRow(); $table->addCell(2000)->addText("单位名称",$fontStyle); $table->addCell(3000)->addText("",$fontStyle); $table->addCell(2000)->addText("详细地址",$fontStyle); $table->addCell(3000)->addText("",$fontStyle); $table->addRow(); $table->addCell(2000)->addText("场所负责人",$fontStyle); $table->addCell(3000)->addText("",$fontStyle); $table->addCell(2000)->addText("联系电话",$fontStyle); $table->addCell(3000)->addText("",$fontStyle); $styleTable2 = array('borderColor'=>'006699', 'borderLeftSize'=>6,'borderRightSize'=>6,'cellMargin'=>80); $fontStyle2 = array('align'=>'center'); // Add table style $PHPWord->addTableStyle('myOwnTableStyle2', $styleTable2); for($i=1;$i<=5;$i++){ $table2 = $section->addTable('myOwnTableStyle2'); $table2->addRow(); $table2->addCell(10000)->addText("服务岗位".$i,$fontStyle); $table3 = $section->addTable('myOwnTableStyle'); $table3->addRow(); $table3->addCell(2000)->addText("岗位内容",$fontStyle2); $table3->addCell(3000)->addText("",$fontStyle2); $table3->addCell(2000)->addText("需求数量",$fontStyle2); $table3->addCell(3000)->addText("",$fontStyle2); $table3->addRow(); $table3->addCell(2000)->addText("服务时数",$fontStyle2); $table3->addCell(3000)->addText("",$fontStyle2); $table3->addCell(2000)->addText("服务周期",$fontStyle2); $table3->addCell(3000)->addText("",$fontStyle2); } $styleTable3 = array('borderColor'=>'006699', 'borderLeftSize'=>6, 'borderBottomSize'=>6,' borderRightSize'=>6,'cellMargin'=>80); $fontStyle3 = array('align'=>'center'); $cellStyle3 = array('borderColor'=>'006699', 'borderRightSize'=>6); // Add table style $PHPWord->addTableStyle('myOwnTableStyle3', $styleTable3); $table4 = $section->addTable('myOwnTableStyle3'); $table4->addRow(2000); $table4->addCell(3333,$cellStyle3)->addText("本单位意见",$fontStyle3); $table4->addCell(3333,$cellStyle3)->addText("主管部门意见",$fontStyle3); $table4->addCell(3334)->addText("集团总部意见",$fontStyle3 ); //Two enter $section->addTextBreak(2); //Add image $section->addImage('logo.jpg', array('width'=>100, 'height'=>100,'align'=>'right')); $objWrite = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWrite->save('index.docx');