<?php namespace app\index\controller; use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style\Table; use PhpOffice\PhpWord\SimpleType\JcTable; use PhpOffice\PhpWord\SimpleType\VerticalJc; class Index { public function index() { // 创建新的Word文档 $phpWord = new PhpWord(); // 添加一个新的空白页 $section = $phpWord->addSection(); // 设置表格的样式 $styleTable = array( 'borderSize' => 6, 'borderColor' => '999999', 'cellMargin' => 50, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER ); $phpWord->addFontStyle('titlestyle', array('bold' => true,'size' => 16,'name' => 'msyh'));//标题的样式 $title = "报名表"; //添加标题 $section->addText($title,'titlestyle', ['alignment' => 'center']); $section->addTextBreak(1); // 表格样式 $styleTable = array( 'borderSize'=>6, 'alignment' => 'center','cellMargin'=>80); // 第一行样式 $styleFirstRow = array('bgColor'=>'f2f2f2' ); $phpWord ->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow); $cellRowContinue = array('vMerge' => 'continue'); // 添加表格 $table = $section->addTable($styleTable); // 添加表头 $table->addRow(900); $table->addCell(8000, ['valign' => 'center','gridSpan'=>4])->addText('基本信息','titlestyle', ['alignment' => 'center']); $table->addRow(); $table->addCell(2000)->addText("1.姓名"); $table->addCell(2000)->addText(""); $table->addCell(2000)->addText("2.性别"); $table->addCell(2000)->addText(""); $table->addRow(900); $table->addCell(2000, ['valign' => 'center'])->addText('3.身份证号码/护照编号'); $table->addCell(2000, ['valign' => 'center','gridSpan'=>3])->addText(''); $table->addRow(); $table->addCell(2000)->addText("4.职称"); $table->addCell(2000)->addText(""); $table->addCell(2000)->addText("5.是否来自基层"); $table->addCell(2000)->addText(""); $table->addRow(); $table->addCell(2000)->addText("6.年龄/岁"); $table->addCell(2000)->addText(""); $table->addCell(2000)->addText("7.联系电话。"); $table->addCell(2000)->addText(""); $table->addRow(900); $table->addCell(2000, ['valign' => 'center'])->addText('8.通讯地址'); $table->addCell(2000, ['valign' => 'center','gridSpan'=>3])->addText(''); // 将Word文档保存到文件 $objWriter = IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('刘佳明.docx'); echo 'Word文档已生成!'; } }
合并单元格的写法
$styleTable = array('borderSize' => 6, 'borderColor' => '999999'); $cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center'); $cellRowContinue = array('vMerge' => 'continue'); $cellColSpan = array('gridSpan' => 2, 'valign' => 'center'); $cellHCentered = array('align' => 'center'); $cellVCentered = array('valign' => 'center'); $PHPWord->addTableStyle('Colspan Rowspan', $styleTable); $table = $section->addTable('Colspan Rowspan'); $table->addRow(); $table->addCell(2000, $cellRowSpan)->addText('A', null, $cellHCentered); $table->addCell(4000, $cellColSpan)->addText('B', null, $cellHCentered); $table->addCell(2000, $cellRowSpan)->addText('E', null, $cellHCentered); $table->addRow(); $table->addCell(null, $cellRowContinue); $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered); $table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered); $table->addCell(null, $cellRowContinue);
附图
本文为看恩吧原创文章,转载无需和我联系,但请注明来自knsay.com