目录

Phpword小技巧

上篇文章写了phpword的基本用法,这篇文章主要写一下phpword的一些别的方法

关于表格

在表格制作中有很多东西需要做比如跨行,跨列,一个单元格填入多个文字或者插入文字图片

1.跨行跨列

这个实例在扩展的例子中就有,就不去重复早轮子了 https://img.cdn.resowolf.com/static/C3082767-59A3-48D7-A00E-FAED00696CBD.jpeg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$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);

https://img.cdn.resowolf.com/static/D4D189C8-5386-4EDB-87AF-D3B2F4090841.jpeg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

$section->addPageBreak();
$section->addText('Table with colspan and rowspan', $header);

$styleTable = ['borderSize' => 6, 'borderColor' => '999999'];
$phpWord->addTableStyle('Colspan Rowspan', $styleTable);
$table = $section->addTable('Colspan Rowspan');

$row = $table->addRow();

$row->addCell(null, ['vMerge' => 'restart'])->addText('A');
$row->addCell(null, ['gridSpan' => 2, 'vMerge' => 'restart',])->addText('B');
$row->addCell()->addText('1');

$row = $table->addRow();
$row->addCell(null, ['vMerge' => 'continue']);
$row->addCell(null, ['vMerge' => 'continue','gridSpan' => 2,]);
$row->addCell()->addText('2');
$row = $table->addRow();
$row->addCell(null, ['vMerge' => 'continue']);
$row->addCell()->addText('C');
$row->addCell()->addText('D');
$row->addCell()->addText('3');

2.一个单元格填入多个文字或者插入文字图片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$table->addRow();
$table->addCell('1134', $cell_row_span)->addText('D412.11周五', $font_style_12, $paragraph_style);
$cell = $table->addCell('8505', $cell_col_span);
$cell->addTextBreak();
$cell->addText('qqwewqwqweeeqqqw', $font_style_12, $paragraph_style_2);
$cell->addText('asdjsadjasdasd',
	$font_style_12,
	$paragraph_style_2);
$cell->addTextBreak();
$cell->addText('cccccwwww',
	$font_style_12,
	$paragraph_style_2);
$cell->addText('lslslslslslqqqsskkks',
	$font_style_12,
	$paragraph_style_2);
$cell->addText('tototototo',
	$font_style_12,
	$paragraph_style_2);

关于图片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//绝对位置
[
	'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
	'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT,
	'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
	'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
]
//相对位置
[
	'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
	'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
	'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
	'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
	'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_LINE,
]

如果这么写

1
2
3
4
5
//绝对位置
[
	'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
	'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT,
]

可以是实现图片充满整个页面的宽度,直接使用上面相对位置的代码也可以实现充满整个页面的宽度这个效果

关于工具

word的有可能需求给得是厘米,英寸等单位,我们在写代码的时候需要转成px或者twips每个值都需要转换,这个样会很烦,但是phpword给出了一个直接可以转换的小工具;使用

1
		\PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
  • cmToTwip. cm -> Twip
  • cmToInch. cm -> 英寸
  • cmToPixel. cm -> 像素
  • cmToPoint. cm -> 磅
  • cmToEmu. cm -> Emu
  • inchToTwip. 英寸 -> Twip
  • inchToCm. 英寸 -> cm
  • inchToPixel. 英寸 -> 像素
  • inchToPoint. 英寸 -> 磅
  • inchToEmu. 英寸 -> Emu
  • pixelToTwip. 像素 -> Twip
  • pixelToCm. 像素 -> cm
  • pixelToPoint. 像素 -> 磅
  • pixelToEmu. 像素 -> Emu
  • pointToTwip. 磅 -> 像素
  • pointToPixel. 磅 -> Emu
  • pointToEmu. 磅 -> Emu
  • emuToPixel.
  • degreeToAngle. 弧度 -> 角度
  • angleToDegree. 角度 -> 弧度
  • htmlToRgb. html -> Rgb