今天發現以前合并單元格并沒有生效,經過研究發現以下結論:
/*
* 3. colspan (gridSpan) and rowspan (vMerge)
* ---------------------
* | | B | |
* | A |--------| E |
* | | C | D | |
* ---------------------
*/
$section->addPageBreak();
$section->addText('Table with colspan and rowspan', $header);
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999');
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center', 'bgColor' => 'FFFF00');
$cellRowContinue = array('vMerge' => 'continue');
$cellColSpan = array('gridSpan' => 2, 'valign' => 'center');
$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
$cellVCentered = array('valign' => 'center');
$spanTableStyleName = 'Colspan Rowspan';
$phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
$table = $section->addTable($spanTableStyleName);
$table->addRow();
$cell1 = $table->addCell(2000, $cellRowSpan);
$textrun1 = $cell1->addTextRun($cellHCentered);
$textrun1->addText('A');
$textrun1->addFootnote()->addText('Row span');
$cell2 = $table->addCell(4000, $cellColSpan);
$textrun2 = $cell2->addTextRun($cellHCentered);
$textrun2->addText('B');
$textrun2->addFootnote()->addText('Column span');
$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);
自己做的
$table = new Table(array(
'borderSize' => 6,
'borderColor' => 'black',
'width' => 5000,
'unit' => TblWidth::PERCENT,
'align' => 'center',
));
$header_style = [
'bold' => true,
'size' => 9
];
$paragraphStyle = [
'align'=>'center',
];
$fontStyle = [
'size' => 9,
];
$table->addRow();
$table->addCell(39*50)->addText('名稱', $header_style, $paragraphStyle);
$table->addCell(3*50)->addText('申請号', $header_style, $paragraphStyle);
$table->addCell(3*50)->addText('類型', $header_style, $paragraphStyle);
$table->addCell(14*50)->addText('繳費年份', $header_style, $paragraphStyle);
$table->addCell(26*50)->addText('繳納年費日期範圍', $header_style, $paragraphStyle);
$table->addCell(3*50)->addText('年費', $header_style, $paragraphStyle);
$table->addCell(10*50)->addText('滞納金', $header_style, $paragraphStyle);
$table->addCell(6*50)->addText('滞納金絕限日', $header_style, $paragraphStyle);
$table->addCell(6*50)->addText('維護費', $header_style, $paragraphStyle);
$total = 0;
if($cases){
foreach($cases as $key=>&$value){
$table->addRow();
$table->addCell(39*50)->addText($value['name'], $fontStyle);
$table->addCell(3*50)->addText($value['apply_no'], $fontStyle);
$table->addCell(3*50)->addText($value['type'], $fontStyle);
$table->addCell(14*50)->addText($value['to_year'], $fontStyle);
$table->addCell(26*50)->addText($value['year_fee_area'], $fontStyle);
$table->addCell(3*50)->addText((int)$value['year_fee'], $fontStyle);
$table->addCell(10*50)->addText((int)$value['late_fee'], $fontStyle);
$table->addCell(6*50)->addText($value['late_fee_end'], $fontStyle);
$table->addCell(6*50)->addText($value['service_price'], $fontStyle);
$total = (int)($value['year_fee'] (int)$value['late_fee'] (int)$value['service_price']);
}
}
$table->addRow();
$table->addCell(94*50, ['valign'=>'center', 'gridSpan' => 8, 'unit'=>'pct'])->addText('合計', $header_style, $paragraphStyle);
$table->addCell(6*50, ['unit'=>'pct'])->addText($total, $header_style, $paragraphStyle);
$templateProcessor->setComplexBlock('table', $table);
從而實現了動态生成word中帶合并單元格(彙總統計)的功能
參考官方sample Sample_09_Tables.php如果模闆文檔裡有多個同名的變量, 需要$templateProcessor->setComplexValue('company', new Text($config['company'])); $templateProcessor->setComplexValue('company', new Text($config['company'])); 多次
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!