tft每日頭條

 > 生活

 > php怎麼設置多個表格

php怎麼設置多個表格

生活 更新时间:2025-01-31 10:27:21

今天發現以前合并單元格并沒有生效,經過研究發現以下結論:

  • 表格的樣式寬度支持四種 NIL、TWIP、AUTO、PERCENT 分别代表無寬度、TWIP word基本單位、自動、百分比
  • 百分比是 1% 代表50份 TWIP
  • 合并單元格,并不會自動實現自動寬度,比如兩行三列單元格, 合并第一行前兩列,最終得到的是第一行兩個cell,第一個cell和原第一個cell寬度相等
  • 如果想要合并單元格 且保證最終合并單元格 的寬度和我們想要的寬度一緻,唯一的辦法是設置表格寬度後,給每行裡的每個單元格設置寬度, 如果設置百分比, 總table 占文檔100% 設為5000,然後按照每個單元格自己想要的百分比去設置N*50
  • 合并單元格是通過設置cell的樣式實現['gridSpan' => 2, 'valign' => 'center']

/* * 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中帶合并單元格(彙總統計)的功能

php怎麼設置多個表格(合并單元格的坑)1

參考官方sample Sample_09_Tables.php如果模闆文檔裡有多個同名的變量, 需要$templateProcessor->setComplexValue('company', new Text($config['company'])); $templateProcessor->setComplexValue('company', new Text($config['company'])); 多次

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved