php整合phpword打印word(合并单元格)

PHPWord中文手册
PHPWord的各种demo(结合需求)

1.php修改word模板样式合并单元格

public function exportWord($param = array(),$NameAll = array(),$TASK_UID='',$Title='',$TaskCode='',$TmpType='',$Time=''){
	global $GLOBAL_SETTING;
	$year = substr($Time,0,4);
	$month = substr($Time,5,2);
	$day = substr($Time,8,2);
	$time = $year."年".$month."月"."$day"."日";
	//读取文件
	// $file = __DIR__ . "/demo.docx";
	// $word = \PhpOffice\PhpWord\IOFactory::load($file);

	$rows=count($param);//总行数
	
	$cellRowSpan = ['vMerge' => 'restart', 'valign' => 'center']; // 设置可跨行,且文字在居中
	$cellRowContinue = ['vMerge' => 'continue']; //使行连接,且无边框线
	$cellHCentered = ['align' => 'center']; //段落居中
	$cellLeft = ['align' => 'left'];//段落居左对齐
	//定义样式数组
	$styleTable = [
		'borderSize'=>6,
		'borderColor'=>'070707',
		'cellMargin'=>80
	];
	$SignTableStyle = [
		'borderSize'=>6,
		'borderColor'=>'070707',
		'cellMargin'=>80,
		];
		//定义第一行的样式
	$styleFirstRow = [
		'borderBottomSize'=>18,
		'borderBottomColor'=>'070707',
		'bgColor'=>'c3bcbb'
	];
	//定义第一行的字体
	$fontStyle = ['bold'=>true,'align'=>'center','size' => 12];

	//定义单元格样式数组  居中
	$styleCell = ['valign'=>'center'];
	$styleCellBTLR = ['valign'=>'center'];

	// require_once "vendor/word/vendor/autoload.php";
	$PHPWord = new \PhpOffice\PhpWord\PhpWord();
	//添加标题样式 标题样式可以自己取名,比如2,3
	$PHPWord->addTitleStyle(2, array('bold' => true, 'size' => 17, 'name' => 'Arial', 'Color' => '333333'), array('align' => 'center'));
	$PHPWord->addTitleStyle(3, array('size' => 12, 'name' => 'Arial', 'Color' => '333333'), array('align' => 'right'));
	$section = $PHPWord->createSection();
	//添加2级标题 2对应上面的标题样式
	$section->addTitle("$Title",2);
	//换行
	$section->addTextBreak(1);	
	//添加日期时间   3对应上面的标题样式
	$section->addTitle("$time",3);
	//添加表格样式
	$PHPWord->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow);
	
	//添加表格
	$table = $section->addTable('myOwnTableStyle');
	
	
	//添加一行
	$table->addRow(900);
	
	//添加单元格
	$table->addCell(2000,$styleCell)->addText('检查区域',$fontStyle,$cellHCentered);
	$table->addCell(2000,$styleCell)->addText('检查内容',$fontStyle,$cellHCentered);
	$table->addCell(2000,$styleCell)->addText('检查情况',$fontStyle,$cellHCentered);
	$table->addCell(2000,$styleCell)->addText('是否合格',$fontStyle,$cellHCentered);
	$table->addCell(2000,$styleCellBTLR)->addText('检查时间',$fontStyle,$cellHCentered);
	$table->addCell(2000,$styleCellBTLR)->addText('检查人员',$fontStyle,$cellHCentered);
	
	//添加更多的行/单元格
	for($i=1;$i<=$rows;$i++){
		$table->addRow();
		if ($param[$i-1]['CheckArea'] == '') {
			$table->addCell(2000, $cellRowContinue);
		}else{
		//添加样式$cellRowSpan来合并单元格
			$table->addCell(2000, $cellRowSpan)->addText($param[$i-1]['CheckArea'],null,$cellHCentered); //设置该列可以跨行,且样式居中
		}

		$table->addCell(2000)->addText($param[$i-1]['CheckItem']);
		$RecordText = "【检查笔录】:".$param[$i-1]['CheckMethod'];
		if ($param[$i-1]['ContentType'] == '选择内容') {
			$text = "【选择内容】:".$param[$i-1]['CheckCondition'].""."".""."$RecordText";
		}else{
			$text = "【输入内容】:".$param[$i-1]['CheckCondition'].""."".""."$RecordText";
		}
		$table->addCell(4000)->addText($text);
		$text2 = $param[$i-1]['CheckISOK'];
		$table->addCell(2000,$styleCell)->addText($text2,null,$cellHCentered);
	
		// $text = ($i%2==0) ? 'X' : '';
		$table->addCell(1000,$styleCell)->addText($param[$i-1]['time'],null,$cellHCentered);
		$table->addCell(1000,$styleCell)->addText($param[$i-1]['name'],null,$cellHCentered);

		
	}

	$section->addTextBreak(2);
	//添加表格样式
	$PHPWord->addTableStyle('signTableStyle',$SignTableStyle);
	//添加表格
	$SignTable = $section->addTable('signTableStyle');

		//添加行
		$SignTable->addRow(500);
		$SignTable->addCell(2000,$styleCell)->addText('保管科签字',$fontStyle,$cellHCentered);
		$SignTable->addCell(2000,$styleCell)->addText($NameAll['BaoGuanName'],$fontStyle,$cellHCentered);
		$SignTable->addCell(2000,$styleCell)->addText('物业签字',$fontStyle,$cellHCentered);
		$SignTable->addCell(2000,$styleCell)->addText($NameAll['WuYeName'],$fontStyle,$cellHCentered);
		$SignTable->addCell(2000,$styleCell)->addText('保安签字',$fontStyle,$cellHCentered);
		$SignTable->addCell(2000,$styleCell)->addText($NameAll['BaoAnName'],$fontStyle,$cellHCentered);
		//存储模板路径
		$filepath=$GLOBAL_SETTING['FILE_PATH'].'/shared/upload/printword/sbwx/'.$TaskCode.'.docx';
		//更新数据库中模板路径
		$sql="UPDATE CSDA_AQJC_TASK AS a SET a.TASK_WORD='$filepath' WHERE a.TASK_UID='$TASK_UID'";
		//带中文的模板名字要换编码格式 这里因为业务需求没用到
		//$filename = iconv("utf-8","gb2312",$Title);
		$filelocal='./shared/upload/printword/sbwx/'.$TaskCode.'.docx';
		//生成word并在前端下载
		$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
		$objWriter->save($filelocal);
		PropelUtil::excute($sql);
	}

2.前端打印下载模板

wps和微软word显示样式各有不同

php整合phpword打印word(合并单元格)_第1张图片

你可能感兴趣的:(PHPWord,php,phpword,合并单元格,打印word,修改word样式)